NiladriBose
NiladriBose

Reputation: 1905

Gmock - matching structures

How can I match value of an element in a union for an input argument e.g - if I mock a method with the following signatiure -

    struct SomeStruct
    {   
        int data1;
        int data2; 
    };

    void SomeMethod(SomeStruct data);

How can I match that mock for this method was called with correct value in argument?

Upvotes: 24

Views: 74428

Answers (5)

JukkaA
JukkaA

Reputation: 211

If there a need to explicitly test for specific value of just one field of a struct (or one "property" of a class), gmock has a simple way to test this with the "Field" and "Property" definitions. With a struct:

EXPECT_CALL( someMock, SomeMethod( Field( &SomeStruct::data1, Eq(expectedValue) )));

Or, alternatively if we have SomeClass (intead of SomeStruct), that has private member variables and public getter functions:

EXPECT_CALL( someMock, SomeMethod( Property( &SomeClass::getData1, Eq(expectedValue) )));

Upvotes: 20

NiladriBose
NiladriBose

Reputation: 1905

After reading through the Google mock documentation in detail, I solved my problem as documented in Defining Matchers section. (An example would have been great!)

So the solution is to use the MATCHER_P macros to define a custom matcher. So for the matching SomeStruct.data1 I defined a matcher:

MATCHER_P(data1AreEqual, ,"") { return (arg.data1 == SomeStructToCompare.data1); }

to match it in an expectation I used this custom macro like this:

EXPECT_CALL(someMock, SomeMethod(data1AreEqual(expectedSomeStruct)));

Here, expectedSomeStruct is the value of the structure.data1 we are expecting.

Note that, as suggested in other answers (in this post and others), it requires the unit under test to change to make it testable. That should not be necessary! E.g. overloading.

Upvotes: 35

BWD
BWD

Reputation: 276

Maybe useless as the question has been answered long time ago but here is a solution that works with any structure and which does not use MATCHER or FIELD.

Suppose we are checking: methodName(const Foo& foo):

using ::testing::_;

struct Foo {
    ...
    ...
};

EXPECT_CALL(mockObject, methodName(_))
    .WillOnce([&expectedFoo](const Foo& foo) {
        // Here, gtest macros can be used to test struct Foo's members
        // one by one for example (ASSERT_TRUE, ASSERT_EQ, ...)
        ASSERT_EQ(foo.arg1, expectedFoo.arg1);
    });

Upvotes: 7

Jordan
Jordan

Reputation: 1645

This is basically answered above but I want to give you one more good example:

// some test type
struct Foo {
    bool b;
    int i;
};

// define a matcher if ==operator is not needed in production
MATCHER_P(EqFoo, other, "Equality matcher for type Foo") {
    return std::tie(arg.b, arg.i) == std::tie(other.b, other.i);
}

// example usage in your test
const Foo test_value {true, 42};
EXPECT_CALL(your_mock, YourMethod(EqFoo(test_value)));

Upvotes: 5

Lilshieste
Lilshieste

Reputation: 2754

Google provides some good documentation on using gmock, full of example code. I highly recommend checking it out:

https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#using-matchers

As you pointed out, a default equality operator (==) isn't automatically created for class types (including PODs). Since this operator is used by gmock when matching parameters, you would need to explicitly define it in order to use the type as you would any other type (as seen below):

    // Assumes `SomeMethod` is mocked in `MockedObject`
    MockedObject foo;
    SomeStruct expectedValue { 1, 2 };

    EXPECT_CALL(foo, SomeMethod(expectedValue));

So, the most straightforward way of dealing with this is to define an equality operator for the struct:

struct SomeStruct
{   
    int data1;
    int data2; 

    bool operator==(const SomeStruct& rhs) const
    {
        return data1 == rhs.data1
            && data2 == rhs.data2;
    }
};

If you don't want to go that route, you can consider using the Field matcher to match the parameter based on the values of its member variables. (If a test is interested in comparing equality between instances of the struct, though, it's a good indication that other code will be interested as well. So it'd likely be worthwhile to just define an operator== and be done with it.)

Upvotes: 7

Related Questions