Google Mock: why NiceMock does not ignore unexpected calls?
Asked Answered
D

1

12

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?

Dunson answered 24/7, 2014 at 1:25 Comment(0)
T
15

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.

Maybe you meant this:

EXPECT_CALL(testMock, command("STAT")).Times(1).WillOnce(Return("+OK 1 2\r\n"));
EXPECT_CALL(testMock, command("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"));

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.

Tadeo answered 24/7, 2014 at 18:15 Comment(2)
Is it OK to use 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
@Dunson - yes, absolutely.Tadeo

© 2022 - 2024 — McMap. All rights reserved.