Is it possible to capture parameters with Google Mock (gmock)?

c++, googlemock, mocking, unit-testing

Solution

You can write a custom action to capture a method parameter by reference (there is a standard `SaveArg` action to capture one by value). But what you want can be achieved in a simpler fashion:

using testing::Property;
using testing::Eq;
EXPECT_CALL(myInterface,
            access(Property(&MyObject::getState, Eq(ACCESS_IN_PROGRESS))));

Problem

I am planning on using Google Mock. I need to capture an object reference so that I can subsequently call some methods from that object. Does Google Mock have any capturing abilities? If not, what are the other choices for C++ unit testing? One choice would be to create my own mock class that captures the object. I am looking for something similar to Java's EasyMock. Example (not real code): ``` Capture<MyObject> capture; EXPECT_CALL(myInterface, access(capture)); instanceUnderTest.setAccessPoint(myInterface); instanceUnderTest.run(); MyObject &capturedObject = capture.getValue(); EXPECT_EQ(ACCESS_IN_PROGRESS, capturedObject.getState()); ```

Original source