Gmock Set Reference Based On Another Parameter

Viewed 21

I have an API that looks like: bool api(Object object, ObjectID& objectID)

I want to use ON_CALL to mock the API so that it takes takes a member from object and set it to objectID and return true. But I can’t figure out how to use SetArgReferee and access the other param with it.

1 Answers

You can pass a lambda to the gmock hooks.

For example using WillByDefault, you should be able to do:

ON_CALL(foo, api).WillByDefault([](Object& object, ObjectID objectID) {
      object.some_member = objectID;
});
Related