1

I am using Gmock in my project. I have the following mock function.

  MOCK_METHOD2(helper,
     bool(const std::string&, std::string*));

Goal is to set value of parameter 1 to parameter 2 for all mock calls.

I did the following.

  std::string temp;

  EXPECT_CALL(
        MockObj,
        helper(_,_))
        .WillRepeatedly(
          DoAll(SaveArg<0>(&temp), //Save first arg to temp
                SetArgPointee<1>(temp), //assign it to second arg
                Return(true)));

However I see that parameter 2 is set to the original value of tmp rather than the value saved. Is there any other way to solve this? I want to make this EXPECT_CALL dynamic rather than doing SetArgPointee<1>("testval").

273K
  • 29,503
  • 10
  • 41
  • 64
balajiboss
  • 932
  • 2
  • 12
  • 20

1 Answers1

0

Standard actions do not support what you need. You will need to write a custom matcher:

ACTION(AssignArg0ToArg1) {
  *arg1 = arg0;
}

And you can then use it in combination with Return(true). More information on custom actions can be found at https://github.com/google/googlemock/blob/master/googlemock/docs/DesignDoc.md.

VladLosev
  • 7,296
  • 2
  • 35
  • 46
  • 2
    Thanks for pointing it out. Ended up using a lambda with Invoke - .WillRepeatedly(DoAll( Invoke([](const std::string& input, std::string* output) { *output = input; }), Return(true))); – balajiboss Aug 01 '17 at 19:27
  • @balajiboss You comment was helpful. Consider constructing it into answer. – ytobi Sep 03 '20 at 12:49