I'm just learning to use Google Mock. It works fine when I test a class to which I can pass the mocked object as a pointer or reference (dependency injection). When I have a class which doesn't use DI, I haven't found a solution.
We currently have a lot of code which doesn't use DI and interfaces, for example:
#include "ReadDbObjects.h"
class ObjectManager
{
public:
ObjectManager()
{
ReadDbObjects db_reader;
mObjects = db_reader.ReadAll();
}
private:
vector<MyObject> mObjects;
}
(this is extremly simplified, but should explain the problem)
We currently test something like this by creating just for the unit test a file called ReadDbObjects.cpp (which includes the original ReadObjects.h with the class declaration), which is statically linked to the unit test. In this file, we mock the ReadAll-Method, so it returns the objects we need for the test. This file is hard to maintain and it needs another file which is known both in the ReadObjects.cpp and the unit test, so values between the test and the mocked ReadObjects-class can be passed in both directions.
- Is there a way to mock ReadObjects using gmock in this example?
- Can gmock be used, when I need to mock a base class? Here we use the same mechanism by linking a mocked cpp base class file into the unit test.