简体   繁体   English

GoogleMock:期待两个方法调用中的任何一个

[英]GoogleMock: Expect either of two method calls

I have a class Foo that references multiple other objects of type IBar .我有一个类Foo ,它引用了IBar类型的多个其他对象。 The class has a method fun that needs to invoke method frob on at least one of those IBar s.该类有一个方法fun需要在这些IBar的至少一个上调用方法frob I want to write a test with mocked IBar s that verifies this requirement.我想用IBar编写一个测试来验证这个要求。 I'm using GoogleMock.我正在使用 GoogleMock。 I currently have this:我目前有这个:

class IBar { public: virtual void frob() = 0; };
class MockBar : public IBar { public: MOCK_METHOD0(frob, void ()); };

class Foo {
  std::shared_ptr<IBar> bar1, bar2;
public:
  Foo(std::shared_ptr<IBar> bar1, std::shared_ptr<IBar> bar2)
      : bar1(std::move(bar1)), bar2(std::move(bar2))
  {}
  void fun() {
    assert(condition1 || condition2);
    if (condition1) bar1->frob();
    if (condition2) bar2->frob();
  }
}

TEST(FooTest, callsAtLeastOneFrob) {
  auto bar1 = std::make_shared<MockBar>();
  auto bar2 = std::make_shared<MockBar>();
  Foo foo(bar1, bar2);

  EXPECT_CALL(*bar1, frob()).Times(AtMost(1));
  EXPECT_CALL(*bar2, frob()).Times(AtMost(1));

  foo.fun();
}

However, this doesn't verify that either bar1->frob() or bar2->frob() is called, only that neither is called more than once.然而,这并不能验证bar1->frob()bar2->frob()是否被调用,只是验证两者都没有被调用多次。

Is there a way in GoogleMock to test for a minimum number of calls on a group of expectations, like a Times() function I can call on an ExpectationSet ?在 GoogleMock 中是否有一种方法可以测试对一组期望的最少调用次数,例如我可以在ExpectationSet上调用Times()函数?

This can be achieved using check points :这可以使用检查点来实现:

using ::testing::MockFunction;

MockFunction<void()> check_point;
EXPECT_CALL(*bar1, frob())
    .Times(AtMost(1))
    .WillRepeatedly(
        InvokeWithoutArgs(&check_point, &MockFunction<void()>::Call);
EXPECT_CALL(*bar2, frob())
    .Times(AtMost(1))
    .WillRepeatedly(
        InvokeWithoutArgs(&check_point, &MockFunction<void()>::Call);
EXPECT_CALL(check_point, Call())
    .Times(Exactly(1));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 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? 在C ++中使用GoogleMock期望引用对象匹配器 - Expect matcher for reference object with GoogleMock in C++ 将方法参数传递给googlemock中的操作 - Pass method parameter to an action in googlemock 期待来自另一个线程的 googlemock 调用 - Expecting googlemock calls from another thread 简单的EXPECT_CALL情况下的googlemock细分错误 - googlemock segmentation fault with simple case of EXPECT_CALL 使用googlemock EXPECT_CALL和shared_ptr? - Using googlemock EXPECT_CALL with shared_ptr? googlemock - 模拟一个返回复杂数据类型的方法 - googlemock - mock a method that returns a complex datatyp 使用 googlemock 模拟正在测试的函数的内部调用 - Mocking internal calls of a function being tested using googlemock GTest和GoogleMock EXPECT_CALL在Windows中失败,使用char * param在Mac上传递 - GTest and GoogleMock EXPECT_CALL Fails in windows, passes on Mac with char * param GoogleMock 尝试使用 EXPECT_CALL 将函数参数设置为特定值 - GoogleMock trying to set a function argument to a specific value using EXPECT_CALL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM