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