Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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());
like image 626
Victor Lyuboslavsky Avatar asked Apr 24 '13 19:04

Victor Lyuboslavsky


People also ask

What is gMock used for?

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.

How do you make a mock gMock?

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.

What is Google mock test?

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.


1 Answers

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))));
like image 133
VladLosev Avatar answered Sep 20 '22 03:09

VladLosev