谷歌模拟:为什么部分订购的期望难以满足总订购?

hae*_*lix 4 c++ expectations googlemock

我主要使用GoogleMock的有序期望,因此所有内容EXPECT_CALL都写在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()));
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,如果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
Run Code Online (Sandbox Code Playgroud)

然后,在测试结束时:

Actual function call count doesn't match EXPECT_CALL(mock2, handleMessage(NotNull()))...
         Expected: to be called once
           Actual: never called - unsatisfied and active
Run Code Online (Sandbox Code Playgroud)

hae*_*lix 5

在GoogleMock学习曲线上取得更多进展后,我会尝试以一般性足以提供帮助的方式回答我自己的问题.

让我们考虑以下完全有序期望的例子:

{
    InSequence s;

    EXPECT_CALL(mock1, methodA(_));     // expectation #1
    EXPECT_CALL(mock2, methodX(_));     // expectation #2

    EXPECT_CALL(mock1, methodA(_));     // expectation #3
    EXPECT_CALL(mock2, methodY(_));     // expectation #4
}
Run Code Online (Sandbox Code Playgroud)

现在,让我们将排序分成两部分.

{
    InSequence s;

    EXPECT_CALL(mock1, methodA(_));     // expectation #1
    EXPECT_CALL(mock2, methodX(_));     // expectation #2
}

{
    InSequence s;

    EXPECT_CALL(mock1, methodA(_));     // expectation #3
    EXPECT_CALL(mock2, methodY(_));     // expectation #4
}
Run Code Online (Sandbox Code Playgroud)

目的是允许来自两个序列的期望"合并",即期望#1作为#2的前提条件,#3作为#4的前提条件但不超过该条件.

但是,以下序列的调用将满足完全有序的期望,但不满足"部分有序"的期望:

mock1.methodA();   // call #1
mock2.methodX();   // call #2
mock1.methodA();   // call #3
mock2.methodY();   // call #4
Run Code Online (Sandbox Code Playgroud)

原因:很明显为什么满足完全有序的期望:这个例子只是按照它们的编写顺序来满足它们.存在InSequence,他们一满意就退休.

但是,"部分有序"方案不起作用,因为#1号呼叫满足期望#3,然后呼叫#2将与期望#2匹配,因为它具有期望#1作为前提条件,所以无法满足期望#2.尽管从技术上讲,期望#1和#3是相同的,但它们以相反的顺序写入,因为它们不属于相同的排序,因此失败.

我觉得Google Mock记录的这种现象还不够好.我还在寻找更好的形式化.我怀疑这里使用的"偏序"概念有问题.