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());
Google's C++ Mocking Framework or GMock for short, is an open sourced, free and extendible library for creating fake objects, use them, set behavior on them without the need to change the class with each and every test.
When using gMock, first, you use some simple macros to describe the interface you want to mock, and they will expand to the implementation of your mock class; next, you create some mock objects and specify its expectations and behavior using an intuitive syntax; then you exercise code that uses the mock objects.
Google Test is a popular C++ unit testing framework developed by Google that can be used together with the closely related mocking extension framework, Google Mock, to test code that conforms to the SOLID principles for object-oriented software design.
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))));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With