0

I have a virtual function defined in the class which is set as MOCK_METHOD inside the mock class.

Is it possible to override the MOCK_METHOD to change the details of the function?

For example, SomeFunction is what I described above, and here the return value is merely set

TEST_F(UnitTest, Test)
{
    EXPECT_CALL(*classObject, SomeFunction()).
      WillOnce(Return(true));
}

Rather than merely setting its return to some bool, could I modify the function itself within this unit test? So like say invoke a method of classObject that returns a bool?

After looking into it, Invoke is something that could be used however upon trying the following, I am getting the error Call to non-static function without an object argument.

TEST_F(UnitTest, Test)
{
    EXPECT_CALL(*classObject, SomeFunction()).
      WillOnce(Invoke(classObject, 
      &ClassObject::SomeClassObjectMethod()));
}
xyf
  • 664
  • 1
  • 6
  • 16
  • Have you look at [action reference](http://google.github.io/googletest/reference/actions.html)? – Jarod42 Aug 20 '22 at 17:21
  • is it the `Invoke` call? When I run this I get an error saying "No matching member. function for call to 'gmock_SomeFunction'` `EXPECT_CALL(*classObject, SomeFunction()).WillOnce( Invoke(classObject->someClassObjectMethod());` – xyf Aug 20 '22 at 17:30
  • 1
    Syntax would be `Invoke(classObject, &ClassObject::someClassObjectMethod)` – Jarod42 Aug 20 '22 at 21:31

0 Answers0