简体   繁体   English

嵌套预期调用时,gtest EXPECT_CALL 不起作用

[英]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.

相关问题 通过 setargreferee 传递向量时,gtest 中未实现 EXPECT_CALL - Unfulfilled EXPECT_CALL in gtest when passing vector by setargreferee 如何使用 gtest 模拟 EXPECT_CALL - How to simulate an EXPECT_CALL with gtest object构造前如何调用EXPECT_CALL gtest宏 - How to call EXPECT_CALL gtest macro before the object construction 在 gtest 中模拟纯虚函数并使用 expect_call 进行测试 - Mocking a pure virtual function in gtest and testing with expect_call GTest测试用例“EXPECT_CALL”编译错误 - GTest test case “EXPECT_CALL” compilation error 使用gtest EXPECT_CALL时的竞争条件段错误,而另一个期望是使用相同的方法 - Race condition segfault when using gtest EXPECT_CALL while another expectation is exercising the same method GMock:EXPECT_CALL没有按预期方式对文件* - GMock: EXPECT_CALL not called as expected for File * GTest和GoogleMock EXPECT_CALL在Windows中失败,使用char * param在Mac上传递 - GTest and GoogleMock EXPECT_CALL Fails in windows, passes on Mac with char * param Googletest (gtest) / googlemock (gmock):为什么“interleav[ing] `EXPECT_CALL()`s and calls to the mock functions”未定义行为? - Googletest (gtest) / googlemock (gmock): Why is “interleav[ing] `EXPECT_CALL()`s and calls to the mock functions” undefined behavior? Gtest:如何在expect_call中每次根据增加的数字返回不同的字符串值? - Gtest: how to return different string value based on an increasing number every time in the expect_call?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM