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)
It there something that I misunderstand, or doing wrong, or that is a bug in Google Mock framework?
EXPECT_CALL(testMock, command(_)).Times(AnyNumber())
if I do not know exactly how many times the method is to be called? I am only interested in the fact that it called exactly once with 'STAT' parameter. – Dunson