Git Product home page Git Product logo

Comments (1)

ikeban avatar ikeban commented on May 18, 2024

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

from googlemock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.