Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Gtest how return different values in ON_CALL?

Is it possible to return different values using ON_CALL WillByDefault? for example

class FooMock {
  MOCK_METHOD0(foo, int());
}

void bar()
{
  FooMock mock;
  int f = 0;
  ON_CALL(mock, foo()).WillByDefault(Return(f));
  EXPECT_TRUE(f==mock.foo()); // this is correct
  f++;
  EXPECT_TRUE(f==mock.foo()); // it is failed, because ON_CALL returns f=0
}

Does exists some way to return new value of variable?

like image 215
kovlet Avatar asked Oct 30 '25 15:10

kovlet


1 Answers

Yes there is a way, change your code to:

ON_CALL(mock, foo())
       .WillByDefault(ReturnPointee(&f));

Read more about ReturnPointee in this link (in the Returning Live Values from Mock Methods section)

like image 87
Old Fox Avatar answered Nov 01 '25 05:11

Old Fox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!