26

I have an interface Itest:

class Itest {
    bool testfunction(vector<int>& v, int& id);
}

I can mock it with:

MOCK_METHOD2(testfunction, bool(vector<int>&, int&))

but how can I set the return values?

I tried:

vector<int> v;
int i;
EXPECT_CALL(testobject, testfunction(_,_, _))
            .WillOnce(testing::SetArgReferee<0>(v))
            .WillOnce(testing::SetArgReferee<1>(i))
            .WillOnce(Return(true));

but then it is called three times..

How do I set these argReferees and the return value one time?

273K
  • 29,503
  • 10
  • 41
  • 64
user3549244
  • 273
  • 1
  • 3
  • 5

1 Answers1

53

You combine several actions together using the DoAll action:

EXPECT_CALL(testobject, testfunction(_, _, _))
    .WillOnce(DoAll(SetArgReferee<0>(v), SetArgReferee<1>(i), Return(true)));

See Google Mock wiki CheatSheet for more info.

vt.
  • 1,325
  • 12
  • 27
VladLosev
  • 7,296
  • 2
  • 35
  • 46