我主要使用 GoogleMock 的有序期望,所以所有EXPECT_CALL
s 都写在对象的范围内testing::InSequence
。
现在我想放宽排序,所以我将期望分成 2 个序列。你会说测试应该通过,但没有——它失败了,抱怨未满足的先决条件。我应该如何推理?
编辑:我的代码的简化版本:
//InSequence s; // uncomment this and it works
for (int i = 1; i <= 2; ++i)
{
{
//InSequence s; // uncomment this and it doesn't work
EXPECT_CALL(mock1, produceMessage(_))
.WillOnce(DoAll(SetArgReferee<0>(val1), Return(false)))
.WillOnce(DoAll(SetArgReferee<0>(val2), Return(false)))
.WillOnce(DoAll(SetArgReferee<0>(val2), Return(false)));
EXPECT_CALL(mock2, handleEvent(A<MyType>()));
EXPECT_CALL(mock2, handleMessage(NotNull()));
}
}
所以如果 InSequence 嵌套在for
循环内,我应该有一个偏序,这是一个宽松的要求,与 InSequence 在外面的情况相比。
我得到的错误:
Mock function called more times than expected - returning default value.
Function call: handleMessage(0xd7e708)
Returns: false
Expected: to be called once
Actual: called twice - over-saturated and active
然后,在测试结束时:
Actual function call count doesn't match EXPECT_CALL(mock2, handleMessage(NotNull()))...
Expected: to be called once
Actual: never called - unsatisfied and active