Hello,

I just started using Gtest/Gmocks and I'm struggling with an example. I have a simple class which has a member a function that returns a value(say 3). I'm trying to mock this test and check if the returned result is 3. For simplicity I wrote everything in a single file:

#include "stdafx.h"
#include "gmock\gmock.h"
#include "gtest\gtest.h"
using ::testing::AtLeast;
using namespace std;

class MyClass{
public:
virtual int retValue() { return 3; }
virtual ~MyClass(){}

class FakeMyClass : public MyClass
public:
MOCK_METHOD0( retValue, int() );

TEST(TestForMyClass, TestRetVal)
FakeMyClass obj3;
EXPECT_EQ(obj3.retValue(), 3);

int _tmain(int argc, _TCHAR* argv[])
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();

Hi @Cantafford ,

This is quite an old issue but still in "open" state and my answer might be useful for other users.

If you create a fake object (in your case FakeMyClass ) you don't want to call original implementation (from MyClass). This is the whole point about making fake objects.

If you execute your code, here is what GMock will print:

GMOCK WARNING:
Uninteresting mock function call - returning default value.
    Function call: retValue()
          Returns: 0
NOTE: You can safely ignore the above warning unless this call should not happen.  Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call.  See https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#knowing-when-to-expect for details.

So GMock is telling you, that value 0 will be returned (hence EXPECT_EQ is not satisfied).
I will repeat it, the whole point is to NOT call original implementation from MyClass (if you want to call original implementation, just use object of original class :) ). If you injected this fake object to another object, and you want to force it to return value you need (let's say 3) you should specify that with GMock commands (EXPECT_CALL). In practice it should look like this:

TEST(TestForMyClass, TestRetVal)
FakeMyClass obj3;
EXPECT_CALL(obj3, retValue()).WillOnce(Return(3));
EXPECT_EQ(obj3.retValue(), 3);

If you still believe, that you want to call implementation from class MyClass, you can always do something like that:

TEST(TestForMyClass, TestRetVal)
FakeMyClass obj3;
EXPECT_CALL(obj3, retValue()).WillOnce(Return(obj3.MyClass::retValue()));
EXPECT_EQ(obj3.retValue(), 3);

Finally, if you don't really care how many times FakeMyClass is called and you just want to return "3" whenever it happens, you can use NiceMock:

TEST(TestForMyClass, TestRetVal)
FakeMyClass obj3;
NiceMock<FakeMyClass> niceMockForFakeClass;
ON_CALL(niceMockForFakeClass, retValue()).WillByDefault(Return(3));
EXPECT_EQ(niceMockForFakeClass.retValue(), 3);

I hope that my explanation helped you.

I think that this issue should be marked as solved and closed unless @Cantafford has some additional questions.

Regards,
Ikeban