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?
- when unit testing. do we consider the class as one unit or it's methods? if methods then shouldn't we mock some methods to be able to test other ones in isolation?
- In my case it is part of the criteria that pureFunc is called n times. It is a callback function that is called with every data packet received. So really the purpose of func is to parse received data and pass it to pureFunc. How can I test that functionality without mocking pureFunc?
– pooya13 Feb 22 '19 at 18:34