I have the following call:
EXPECT_CALL(myMock, myFunction(someSpecifiedParameter, _, _))
.WillOnce(DoAll(SaveArg<2>(&bufferSize), Return(make_pair(Success, bufferSize))));
I'm trying to return whatever value that is passed as the second _
as my second element in the pair. Is it the best (or at least right) way to do it?
This bufferSize variable was declared in the test class.
EDIT:
Putting in other words:
Suppose I have the following:
class object{
pair<int, int> f(int x);
}
object obj;
constexpr int fixedValue = 5;
EXPECT_CALL(obj, f(_)).WillOnce(
Return(make_pair(fixedValue, <PARAMETER PASSED TO F>));
CodePudding user response:
Your way is right, you deanonymize the value in the third parameter. In my opinion using a lambda or a custom actions is the more preferable way. A stored lambda or an action can be reused in other expectations.
EXPECT_CALL(myMock, myFunction(someSpecifiedParameter, _, _))
.WillOnce(WithArgs<0, 2>([](Type1 Success, Type2 bufferSize) {
return make_pair(Success, bufferSize);
}));
or
ACTION_P2(ReturnPair, Success, bufferSize) {
return make_pair(Success, bufferSize);
}
EXPECT_CALL(myMock, myFunction(someSpecifiedParameter, _, _))
.WillOnce(WithArgs<0, 2>(ReturnPair));
More specialized thus less preferable
EXPECT_CALL(myMock, myFunction(someSpecifiedParameter, _, _))
.WillOnce(WithArg<2>([](Type2 bufferSize) {
return make_pair(someSpecifiedParameter, bufferSize);
}));
or
ACTION_P(ReturnPair, bufferSize) {
return make_pair(someSpecifiedParameter, bufferSize);
}
EXPECT_CALL(myMock, myFunction(someSpecifiedParameter, _, _))
.WillOnce(WithArg<2>(ReturnPair));