1
  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.
273K
  • 29,503
  • 10
  • 41
  • 64
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

1 Answers1

1

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?.

Yang
  • 7,712
  • 9
  • 48
  • 65
  • I made the changes you suggested. The problem is that when I say WillOnce.(Return(10)), I think it should fail because it is returning 1000. Please see the changes that I have made now in the OP and please tell why it is NOT failing. I have made changes in `gtest.cpp` also. – Aquarius_Girl Mar 27 '19 at 05:05
  • Thanks for clarifying. I think an explanation of mocking would help here. By mocking, we mean we're creating an object for which we can control 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." – Yang Mar 27 '19 at 05:24
  • yes, but then isn't it expected to see whether it does return -100 and 10? Why is it not failing? – Aquarius_Girl Mar 27 '19 at 05:25
  • 1
    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. – Yang Mar 27 '19 at 05:26
  • 1
    In your example, the first call returns -100, the second call returns 10. That matches the expectations, right? The `my_inet.cpp` implementation is overridden. – Yang Mar 27 '19 at 05:26
  • I finally get it now! You mean to say that by `WillOnce.Return` we actually hardcode the return value there rather than in the body of the function. – Aquarius_Girl Mar 27 '19 at 05:32
  • 1
    Right, there're some other questions on mocking which may help you understand the concept: https://stackoverflow.com/questions/3622455/what-is-the-purpose-of-mock-objects, https://stackoverflow.com/questions/2665812/what-is-mocking. – Yang Mar 27 '19 at 05:37
  • I'm glad it helps. – Yang Mar 27 '19 at 06:01