Search code examples
c++googletestvirtual-functionsgooglemock

GMock's `WillOnce` with `Return` does not fail on wrong return value


  1. I have created a mock of an external socket api in my_inet.cpp file.
  2. GMock functions for that socket api is in mock.h file.
  3. I am using my created socket api of my_inet in server.cpp file.
  4. The test is written in gtest.cpp.

Problem is that in my_inet.cpp I am returning 1000 and in gtest.cpp I have written .WillOnce( Return( 10 ));, and it does NOT fail.

Why?


gtest.cpp

TEST(HelloTest, HelloReturnsOne)
{
    MockMyTCPAPI obj_myTCP;

    EXPECT_CALL( obj_myTCP, hello())
    .Times( 2 )
    .WillOnce( Return( -100 ))
    .WillOnce( Return( 10 ));

    Server obj_server( &obj_myTCP );

    EXPECT_EQ( obj_server.hi(), -100 );
    EXPECT_EQ( obj_server.hi(), 10 );
}

mock.h

#include "my_inet.h"
#include <gmock/gmock.h>

class MockMyTCPAPI : public MyTCPAPI {
 public:
  MOCK_METHOD0( hello,  int());
  MOCK_METHOD3( socket, int(int arg1, int arg2, int arg3));
  MOCK_METHOD3( bind,   int(int arg1, int arg2, int arg3));
  MOCK_METHOD2( listen, int(int arg1, int arg2));
  MOCK_METHOD3( accept, int(int arg1, int arg2, int arg3));
  MOCK_METHOD2( send,   int(int arg1, int arg4));
};

my_inet.cpp

int MyTCPAPI::hello()
{
    return 1000;
}

server.cpp

int Server::hi()
{
    return myTCPAPI->hello();
}

Output:

[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from HelloTest
[ RUN      ] HelloTest.HelloReturnsOne
[       OK ] HelloTest.HelloReturnsOne (0 ms)
[----------] 1 test from HelloTest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[  PASSED  ] 1 test.

Solution

  • In server.cpp, myTCPAPI->hello() will return -100 and 10, but Server::hi is not returning them; it always returns 1.

    Could you try:

    int Server::hi() {
      return myTCPAPI->hello();
    }
    

    Updated answer on mocking

    By mocking, we create an object for which we can control (not test) its return value. For example, the EXPECT_CALL statement says "the hello method of obj_myTCP will be called twice. For the first call, return -100; for the second call, return 10." In your example, the first call returns -100, the second call returns 10. That matches the expectations. The my_inet.cpp implementation is overridden.

    The use of a mock object is to inject a return value, not test its return value. Its benefit is more obvious if you imagine mocking a timing object. In this case, you can control the time to return instead of relying on a real clock.

    For more information on mocking, please refer to What is the purpose of mock objects? and What is Mocking?.