Google Mock:为什么NiceMock不忽略意外呼叫? [英] Google Mock: why NiceMock does not ignore unexpected calls?
问题描述
我将Google Mock 1.7.0与Google Test 1.7.0一起使用.问题是,当我使用NiceMock时,由于意外的模拟函数调用(根据Google Mock文档应被NiceMock忽略)而导致测试失败. 代码如下:
I am using Google Mock 1.7.0 with Google Test 1.7.0. The problem is when I use NiceMock I get test failures because of unexpected mock function call (which should be ignored by NiceMock as per Google Mock documentation). The code looks like this:
// Google Mock test
#include <gtest/gtest.h>
#include <gmock/gmock.h>
using ::testing::Return;
using ::testing::_;
class TestMock {
public:
TestMock() {
ON_CALL(*this, command(_)).WillByDefault(Return("-ERR Not Understood\r\n"));
ON_CALL(*this, command("QUIT")).WillByDefault(Return("+OK Bye\r\n"));
}
MOCK_METHOD1(command, std::string(const std::string &cmd));
};
TEST(Test1, NiceMockIgnoresUnexpectedCalls) {
::testing::NiceMock<TestMock> testMock;
EXPECT_CALL(testMock, command("STAT")).Times(1).WillOnce(Return("+OK 1 2\r\n"));
testMock.command("STAT");
testMock.command("QUIT");
}
但是当我运行测试时,它失败并显示以下消息:
But when I run the test it fails with the following message:
[ RUN ] Test1.NiceMockIgnoresUnexpectedCalls
unknown file: Failure
Unexpected mock function call - taking default action specified at:
.../Test1.cpp:13:
Function call: command(@0x7fff5a8d61b0 "QUIT")
Returns: "+OK Bye\r\n"
Google Mock tried the following 1 expectation, but it didn't match:
.../Test1.cpp:20: EXPECT_CALL(testMock, command("STAT"))...
Expected arg #0: is equal to "STAT"
Actual: "QUIT"
Expected: to be called once
Actual: called once - saturated and active
[ FAILED ] Test1.NiceMockIgnoresUnexpectedCalls (0 ms)
是我误解或做错了什么,还是Google Mock框架中的错误?
It there something that I misunderstand, or doing wrong, or that is a bug in Google Mock framework?
推荐答案
仅当在方法上未设置期望时,NiceMock和StrictMock之间的区别才起作用.但是您已经告诉Google Mock期望使用参数"QUIT"
一次调用command
.看到第二个电话时,就会抱怨.
Distinctions between NiceMock and StrictMock only come into play if there are no expectations set on the method. But you you have told Google Mock to expect a single call to command
with the argument "QUIT"
. When it sees the second call, it complains.
也许你是这个意思:
EXPECT_CALL(testMock, command("STAT")).Times(1).WillOnce(Return("+OK 1 2\r\n"));
EXPECT_CALL(testMock, command("QUIT"));
这将导致两次调用-一次使用"STAT"作为参数,一次使用"QUIT".或这样:
which will expect two calls - one with "STAT" as a parameter, and one with "QUIT". Or this:
EXPECT_CALL(testMock, command(_));
EXPECT_CALL(testMock, command("STAT")).Times(1).WillOnce(Return("+OK 1 2\r\n"));
,它将期望使用参数"STAT"
的单个请求,以及使用参数"STAT"
以外的参数的另一个调用.在这种情况下,期望的顺序很重要,因为EXPECT_CALL(testMock, command(_))
可以满足任何调用,包括"STAT"
的调用(如果它在另一个期望之后).
which will expect a single one with the parameter "STAT"
and one other call with a parameter other than "STAT"
. The order of expectations in this case is important as EXPECT_CALL(testMock, command(_))
will satisfy any calls, including the one with "STAT"
if it comes after the other expectation.
这篇关于Google Mock:为什么NiceMock不忽略意外呼叫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!