I want to test an abstract class with:
- Pure virtual methods that should be overridden in sub-classes
- Non-pure virtual methods that use the pure virtual methods (as opposed to this question)
class Fu
{
public:
virtual void func(int count, std::string data)
{
for(int i = 0; i < count; i++)
{
pureFunc(data);
}
}
virtual void pureFunc(std::string data) = 0;
}
Now to test func, I want to make sure that it calls pureFunc count
times with argument data
. What I have done is to subclass Fu
and also create a Mock
:
class TestFu : public Fu
{
public:
virtual void pureFunc(std::string data) override {};
}
class MockFu : public TestFu
{
public:
MOCK_METHOD1(pureFunc, void(std::string));
}
And to test I do the following:
MockFu mock_fu;
EXPECT_CALL(mock_fu, pureFunc("test"))
.Times(10);
mock_fu.func(10, "test");
However I was wondering if the above is a valid pattern. My worry is that I am testing the mock class as opposed to the class or it's non-abstract sub-class.
To sum up:
- Is the above pattern valid? If yes is it ok that I am testing a mock object? If no how do I achieve my goal?
- In general is it valid practice to mock some parts of a class to unit test other parts? If so what is the common practice to do so?