我主要使用GoogleMock的有序期望,因此所有EXPECT_CALL
都写在testing::InSequence
对象的范围内。Google Mock:为什么部分排序的期望难以满足总排序?
现在我想放松顺序,所以我分两个序列的期望。你会说测试应该通过,但没有 - 它失败了,抱怨未满足的先决条件。我应该如何推理?
编辑:我的代码的简化版本:
//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
你能写一些示例代码吗? – 2014-10-02 12:36:48
只是添加了代码。 – haelix 2014-10-02 16:54:52
补充说明:在第三期望附加'.RetiresOnSaturation()'可以修复部分订单情况。但不是我现实生活中的情况,这更复杂。 – haelix 2014-10-02 16:56:20