我主要使用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