gpt4 book ai didi

c++ - 交织 EXPECT_CALL() 和对模拟函数的调用

转载 作者:太空狗 更新时间:2023-10-29 22:58:34 26 4
gpt4 key购买 nike

Google Mock documentation说:

Important note: Google Mock requires expectations to be set before the mock functions are called, otherwise the behavior is undefined. In particular, you mustn't interleave EXPECT_CALL()s and calls to the mock functions.

有人知道这个限制背后的任何细节吗?然而,我有许多单元测试肯定违反了这条规则,但似乎运行正常。

最佳答案

交织 EXPECT_CALL() 和对模拟函数的调用是未定义的行为

(由谷歌确认,gmock 的制造商,here)

我不同意 @Marko Popovic's assessment 的观点,并且相信他所做的是碰巧有效的未定义行为。 我亲眼看到他在做什么,交错调用,看起来工作正常。但我认为这是未定义的行为。

尽管如此,我需要谷歌的澄清,所以我在这里打开了这个问题:https://github.com/google/googletest/issues/2828。请给它投票以引起注意,因为我希望 Googletest 团队自己澄清这一点。

更新:Google has responded,并声明@Marko Popovic 的回答依赖于未定义的行为。然而,这是一个非常常见的陷阱,因为正如 Marko 指出的那样,而且正如我所看到的那样,它确实有效(至少在大多数时候是这样)。问题在于它依赖于未定义的 gmock 行为。

undefined behavior 的问题是它经常工作,但在技术上不正确,可能有错误,可能导致不稳定的测试,并且在将来 gmock 更新时可能会因未知原因而中断在将来。简而言之:未定义的行为不是面向 future 的,也不是跨平台的。它也可能导致竞争条件或不总是有效。因此,不要这样做。听谷歌。他们陈述以下内容的陈述实际上是正确的:

In particular, you mustn't interleave EXPECT_CALL()s and calls to the mock functions (https://github.com/google/googletest/blob/master/googlemock/docs/for_dummies.md#using-mocks-in-tests)


回到我原来的答案:

在我的另一个回答中,我谈了很多很多关于如何正确使用多个 EXPECT_CALL() 的问题,我声明交错是不行的:google mock - can I call EXPECT_CALL multiple times on same mock object?

Here is a quote from my own writing :

Question 3: Can I call EXPECT_CALL to set some expectations on a mock method, call the mock method, then call EXPECT_CALL on the method again to change the expectations, then call the mock method again?

This question wasn't even explicitly asked by the OP, but the only reason I found this page is because I was searching for this answer for many hours and couldn't find it. My Google search was "[gmock multiple expect_call][10]." Therefore, others asking this question will also fall on this page and need a conclusive answer.

A: NO, you can NOT do this! Although it may seem to work in testing, according to Google, it produces undefined behavior. See general rule #2 above!

"Important note: gMock requires expectations to be set before the mock functions are called, otherwise the behavior is undefined. In particular, you mustn't interleave EXPECT_CALL()s and calls to the mock functions" (https://github.com/google/googletest/blob/master/googlemock/docs/for_dummies.md#using-mocks-in-tests)

因此,这是不允许的!

下面的代码示例也是未定义的行为。

来自:https://google.github.io/googletest/gmock_cheat_sheet.html#verifying-and-resetting-a-mock(已强调):

Do not set new expectations after verifying and clearing a mock after its use. Setting expectations after code that exercises the mock has undefined behavior. See Using Mocks in Tests for more information.

(感谢 @nnovich-OK for pointing the above quote out in the comments )。

此外,从这里:https://google.github.io/googletest/gmock_for_dummies.html#using-mocks-in-tests(原始引用中的原始重点):

Important note: gMock requires expectations to be set before the mock functions are called, otherwise the behavior is undefined. Do not alternate between calls to EXPECT_CALL() and calls to the mock functions, and do not set any expectations on a mock after passing the mock to an API.

This means EXPECT_CALL() should be read as expecting that a call will occur in the future, not that a call has occurred. Why does gMock work like that? Well, specifying the expectation beforehand allows gMock to report a violation as soon as it rises, when the context (stack trace, etc) is still available. This makes debugging much easier.

因此,以下也是未定义的 gmock 行为:

也许这不是未定义的行为!?我添加的只是 Mock::VerifyAndClearExpectations(&myMockObj)

TEST(FooTest, testCaseName)
{
MyMock myMockObj;
...
EXPECT_CALL(myMockObj, myMethod(_)).WillOnce(Return(true));
testMethod();
ASSERT_THAT(...);

Mock::VerifyAndClearExpectations(&myMockObj); // <== NOTICE THIS ADDED LINE!
EXPECT_CALL(myMockObj, myMethod(_)).WillOnce(Return(false));
testMethod();
ASSERT_THAT(...);
}

引用资料:

  1. @Marko Popovic's assessment
  2. 我要求澄清的 Googletest 问题:https://github.com/google/googletest/issues/2828
  3. [我自己的回答] google mock - can I call EXPECT_CALL multiple times on same mock object?

关于c++ - 交织 EXPECT_CALL() 和对模拟函数的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40089204/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com