Pass method method to action in googlemock

I am using Gmock to call a method that returns an item from the QList's heightFromCsvResult value as an out parameter. Here's the actual code:

EXPECT_CALL(*_mockAttributeRepository, getGeometryHeightValues(_,_))
    .WillOnce(DoAll(SetArgReferee<1>(heightsFromCsvResult.at(0)), Return(true)))
    .WillOnce(DoAll(SetArgReferee<1>(heightsFromCsvResult.at(1)), Return(true)))
    .WillOnce(DoAll(SetArgReferee<1>(heightsFromCsvResult.at(2)), Return(true)))
    .WillOnce(DoAll(SetArgReferee<1>(heightsFromCsvResult.at(3)), Return(true)));

      

First parameter lineNumber from

getGeometryHeightValues(int lineNumber, QPair<QString, QString>) 

      

- index for heightsFromCsvResult.at (line number).

Now I'm trying to use Gmock in a more general way:

int* mockLineNumber = new int;
EXPECT_CALL(*_mockAttributeRepository, getGeometryHeightValues(_,_))
   .Times(4)
   .WillRepeatedly(DoAll(SaveArg<0>(mockLineNumber), SetArgReferee<1>(heightsFromCsvResult.at(*(mockLineNumber))), Return(true)));

      

But this code doesn't work because mockLineNumber is never written by Gmock. But EXPECT_CALL is executed.

Does anyone see a problem in the code?

+3


source to share


1 answer


Using a set of variables with SaveArg inside the same EXPECT_CALL doesn't seem to work, you can instead take a custom action that sets the second argument using the 0 argument as the index in the passed container.

ACTION_P(SetArg1WithValueFromPassedArgumentUsingArg0AsIndex, param)
{
  arg1 = param.at(arg0);
} 

int* mockLineNumber = new int;
EXPECT_CALL(*_mockAttributeRepository, getGeometryHeightValues(_,_))
   .Times(4)
   .WillRepeatedly(DoAll(SaveArg<0>(mockLineNumber), SetArg1WithValueFromPassedArgumentUsingArg0AsIndex(heightsFromCsvResult), Return(true)));

      



If you don't need mockLineNumber for something else, you can remove that part.

+4


source







All Articles