gtest EXPECT_CALL does not working when expected call is nested
Look at such code snippet:
class Foo {
public:
Foo() {
}
void invoke_init(const int i) {
init(i);
}
private:
void init(const int i) {
std::cout << "init " << i << std::endl;
}
};
class MockedFoo : public Foo {
public:
MOCK_METHOD(void, init, (const int));
};
TEST(Foo, TestInitCalled) {
MockedFoo mock;
mock.invoke_init(1);
EXPECT_CALL(mock, init(1));
}
As expected, init()
is called and i see corresponding output. But the test is failed. Why? What is wrong there?
Foo::init
needs to be protected instead of private. It also needs to be virtual.
Without protected
as its visibility attribute, it can't really be overridden in the inherited class. Without virtual, gmock can't do much with it either.
Instead of this:
private:
void init(const int i) {
std::cout << "init " << i << std::endl;
}
This:
protected:
virtual void init(const int i) {
std::cout << "init " << i << std::endl;
}