I am new to testing and TDD, but decided to try and learn. I'm currently trying to develop a SettingsManager class that will handle my application settings. It will save the state of the application, and when it closes the SettingsManager, it will save the state using QSettings (and will read when it starts). Now I want to mock QSettings, so my tests will not depend on a random state. However, I could not find a reasonable way to mock / wrap it up, because the method (QSettings :: value ()) I need is not virtual.
Perhaps I'm doing something conceptually wrong? Or is there a way to make fun of this non-virtual method call?
Example: suppose I have this class:
class SettingsManager { private: QSettings *settings; bool m_objectsVisible; public: SettingsManager(QSettings *settings) { this->settings = settings; } void readSettings() { m_objectsVisible = settings.value("Settings/ObjectsVisible").toBool(); } bool objectsVisible() { return m_objectsVisible; } };
And I want to test it this way (I used the Hippomocks syntax just to give an idea)
void TestObjectsAreVisible() { MockRepository mocks; QSettings *settingsMock = mocks.ClassMock<QSettings>(); mocks.ExpectCall(settingsMock , QSettings::value).With("Settings/ObjectsVisible").Return(true); SettingsManager *sManager = new SettingsManager(settingsMock); sManager->readSettings(); assertTrue(sManager->objectsVisible); }
source share