Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gmock multiple in-out parameters SetArgReferee

Tags:

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?

like image 901
user3549244 Avatar asked Apr 18 '14 14:04

user3549244


People also ask

What is SetArgReferee?

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.

What is expect_ call in gMock?

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).

How do you mock in gMock?

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.


1 Answers

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.

like image 135
VladLosev Avatar answered Sep 28 '22 01:09

VladLosev