- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
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 callEXPECT_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(...);
}
关于c++ - 交织 EXPECT_CALL() 和对模拟函数的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40089204/
我正在寻找一个好的压缩算法或库,让我可以将多个压缩数据流交织成一个数据流,不会造成性能或压缩损失。 更多背景信息:我一直在研究一种专用于特定应用程序的压缩格式。这种压缩格式对数据执行一堆特定领域的分析
Google Mock documentation说: Important note: Google Mock requires expectations to be set before the m
我想交织(?)两个字符串,例如: string A = 'HELLO WORLD!' string B = '66666666666666666666' //twenty 6's output = '
我如何按列交织 numpy 矩阵。 给出这个例子: >>> import numpy as np >>> a = np.zeros((3,3)) >>> b = np.ones((3,3)) >>>
我正在尝试按照交错 GStreamer 文档中的说明交错两个音频文件: gst-launch interleave name=i ! audioconvert ! wavenc ! filesink
我有两个列表,保证第一个比第二个多一个项目。我想知道创建一个新列表的最 Pythonic 方法,该列表的偶数索引值来自第一个列表,奇数索引值来自第二个列表。 # example inputs list
我是一名优秀的程序员,十分优秀!