[英]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.正如预期的那样,调用了
init()
并且我看到了相应的 output。 But the test is failed.但是测试失败了。 Why?
为什么? What is wrong there?
那里有什么问题?
Foo::init
needs to be protected instead of private . Foo::init
需要保护而不是private 。 It also needs to be virtual .它还需要是虚拟的。
Without protected
as its visibility attribute, it can't really be overridden in the inherited class.如果没有
protected
作为其可见性属性,则不能在继承的 class 中真正覆盖它。 Without virtual, gmock can't do much with it either.如果没有 virtual,gmock 也不能做太多事情。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.