Gmock - 检查是否使用特定参数调用方法
Gmock - Check that a method is not called with a particular argument
有一个方法只有一个字符串参数virtual void method(const std::string& str) = 0;
在某些情况下,我需要确保没有使用特定参数调用此方法,例如“321”。
如果方法在某处被调用为 ifce->method("123");
并且在测试中我做 EXPECT_CALL(mock, method("321")).Times(0);
那么测试失败:
Expected arg #0: is equal to "321"
Actual: "123"
Expected: to be never called
Actual: never called - saturated and active
[ FAILED ] test.invokeMethodWithDiferentParameters (0 ms)
如何正确操作?
要么使用testing::NiceMock
,它会忽略所有无趣的调用
NiceMock<MockFoo> mock;
EXPECT_CALL(mock, method("321")).Times(0);
或添加几个期望值
EXPECT_CALL(mock, method(_)).Times(AtLeast(1));
EXPECT_CALL(mock, method("321")).Times(0);
有一个方法只有一个字符串参数virtual void method(const std::string& str) = 0;
在某些情况下,我需要确保没有使用特定参数调用此方法,例如“321”。
如果方法在某处被调用为 ifce->method("123");
并且在测试中我做 EXPECT_CALL(mock, method("321")).Times(0);
那么测试失败:
Expected arg #0: is equal to "321"
Actual: "123"
Expected: to be never called
Actual: never called - saturated and active
[ FAILED ] test.invokeMethodWithDiferentParameters (0 ms)
如何正确操作?
要么使用testing::NiceMock
,它会忽略所有无趣的调用
NiceMock<MockFoo> mock;
EXPECT_CALL(mock, method("321")).Times(0);
或添加几个期望值
EXPECT_CALL(mock, method(_)).Times(AtLeast(1));
EXPECT_CALL(mock, method("321")).Times(0);