google mock - EXPECT_CALL 即使函数有不同的参数也会触发
google mock - EXPECT_CALL triggers even though function has different argument
我有 EXPECT_CALL(MockObj, func("abc")).Times(1)
并且 MockObj
是 NiceMock
在我测试的函数中,除了MockObj.func("abc")
之外,还有一个调用MockObj.func("def")
。
我希望合理的做法是 Google 模拟说
oh look we call func("def")
but the arguments do not match the
EXPECT_CALL
; nothing to see here
而是 "complains":
unknown file: Failure
Unexpected mock function call - taking default action specified at:
C:/work/unit_test.cpp:36:
Function call: func(84bf3d9 pointing to "def")
Returns: 1
Google Mock tried the following 1 expectation, but it didn't match:
unit_test.cpp:50: EXPECT_CALL(MockObj, func("abc"))...
Expected arg #0: is equal to 84c8b96 pointing to "abc"
Actual: 84bf479 pointing to "def"
Expected: to be called once
Actual: called once - saturated and active
首先注意到因为调用了MockObj.func("abc")
所以期望还是满足了
我理解 GMock 抛出错误的原因:我在 func
上声明了一个期望,因此它试图将对 func
的调用与期望相匹配,但它与参数不匹配,所以 错误
很好。
但是为什么 GMock 会抛出 错误?由于参数不匹配,为什么选择此行为,即
throw an error if the function matches existing EXPECT_CALL
but not the arguments
您的问题 "But why does GMock throw an error? " 的直接答案是:因为它就是这样设计的。
AFAIU googlemock - 你的 googlemock 是这样工作的:
EXPECT_CALL(MockObj, func("abc"));
- 意思是:"I am interested in Obj::func
in my tests"
MockObj
为 NiceMock
表示:"I do not care about any other functions of Obj
that are not explicit subject of EXPECT_CALL"
所以 - 如果您的测试代码调用 func("def")
- 那么 google-mock 会这样做:
- 我们对
func
感兴趣 - 让我们检查一下
- 我们不希望 "def" - 所以未通过测试
可能你知道你应该这样做:
EXPECT_CALL(MockObj, func(StrNe("abc"))).Times(AnyNumber()); // meaning - do not care
EXPECT_CALL(MockObj, func("abc")); // TImes(1) is default - you might skip it
我有 EXPECT_CALL(MockObj, func("abc")).Times(1)
并且 MockObj
是 NiceMock
在我测试的函数中,除了MockObj.func("abc")
之外,还有一个调用MockObj.func("def")
。
我希望合理的做法是 Google 模拟说
oh look we call
func("def")
but the arguments do not match theEXPECT_CALL
; nothing to see here
而是 "complains":
unknown file: Failure
Unexpected mock function call - taking default action specified at:
C:/work/unit_test.cpp:36:
Function call: func(84bf3d9 pointing to "def") Returns: 1
Google Mock tried the following 1 expectation, but it didn't match:
unit_test.cpp:50: EXPECT_CALL(MockObj, func("abc"))...
Expected arg #0: is equal to 84c8b96 pointing to "abc"
Actual: 84bf479 pointing to "def"
Expected: to be called once
Actual: called once - saturated and active
首先注意到因为调用了MockObj.func("abc")
我理解 GMock 抛出错误的原因:我在 func
上声明了一个期望,因此它试图将对 func
的调用与期望相匹配,但它与参数不匹配,所以 错误
很好。
但是为什么 GMock 会抛出 错误?由于参数不匹配,为什么选择此行为,即
throw an error if the function matches existing
EXPECT_CALL
but not the arguments
您的问题 "But why does GMock throw an error? " 的直接答案是:因为它就是这样设计的。
AFAIU googlemock - 你的 googlemock 是这样工作的:
EXPECT_CALL(MockObj, func("abc"));
- 意思是:"I am interested inObj::func
in my tests"MockObj
为NiceMock
表示:"I do not care about any other functions ofObj
that are not explicit subject of EXPECT_CALL"
所以 - 如果您的测试代码调用 func("def")
- 那么 google-mock 会这样做:
- 我们对
func
感兴趣 - 让我们检查一下 - 我们不希望 "def" - 所以未通过测试
可能你知道你应该这样做:
EXPECT_CALL(MockObj, func(StrNe("abc"))).Times(AnyNumber()); // meaning - do not care
EXPECT_CALL(MockObj, func("abc")); // TImes(1) is default - you might skip it