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?
SetArgReferee<N>(value) Assign value to the variable referenced by the N -th (0-based) argument. SetArgPointee<N>(value) Assign value to the variable pointed by the N -th (0-based) argument.
EXPECT_CALL not only defines the behavior, but also sets an expectation that the method will be called with the given arguments, for the given number of times (and in the given order when you specify the order too).
first, you use some simple macros to describe the interface you want to mock, and they will expand to the implementation of your mock class; next, you create some mock objects and specify its expectations and behavior using an intuitive syntax; then you exercise code that uses the mock objects.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With