【发布时间】:2014-09-15 10:00:17
【问题描述】:
我将 Google Mock 1.7.0 与 Google Test 1.7.0 一起使用。问题是当我使用 NiceMock 时,由于意外的模拟函数调用而导致测试失败(根据 Google Mock 文档,NiceMock 应该忽略它)。 代码如下所示:
// 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");
}
但是当我运行测试时它失败并显示以下消息:
[ 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 框架中的错误?
【问题讨论】:
标签: c++ unit-testing googletest googlemock