让我们考虑一下这样的gmock代码:
using namespace ::testing;
struct TestSuite : Test
{
StrictMock<DependencyMock> dependency;
void expectCallingDependency()
{
EXPECT_CALL(dependency, call());
}
SomeSut sut{dependency};
};
TEST_F(TestSuite, shouldCallDependencyTwice)
{
expectCallingDependency();
sut.someAction1(); //calls dependency.call() inside
expectCallingDependency();
sut.someAction2(); //calls dependency.call() also
}
我遇到这样的错误:
Mock function called more times than expected - returning directly.
Function call: call()
Expected: to be called once
Actual: called twice - over-saturated and active
Actual function call count doesn't match EXPECT_CALL(dependency, call())...
Expected: to be called once
Actual: never called - unsatisfied and active
我知道它仍然尝试匹配第一个EXPECT_CALL
,所以我添加了RetiresOnSaturation
,但没有效果:
Expected: the expectation is active
Actual: it is retired
Expected: to be called once
Actual: called once - saturated and retired
Actual function call count doesn't match EXPECT_CALL(dependency, call())...
Expected: to be called once
Actual: never called - unsatisfied and active
但是,使用RetiresOnSaturation()并在调用expectCallingDependency()
之前移动第二个sut.someAction1()
使一切正常-但是我希望在someAction1()
之后有这个期望,因为在期望之上,它不强制执行someAction1()
调用一次
它对我有用。问题可能出在其他地方。