Google provides good documentation on using gmock, complete with sample code. I highly recommend checking this out:
https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#using-matchers
As you pointed out, the default equality operator ( == ) is not automatically created for class types (including POD). Since this operator is used by gmock when matching parameters, you need to explicitly define it in order to use the type just like any other type (as shown below):
// Assumes 'SomeMethod' is mocked in 'MockedObject' MockedObject foo; SomeStruct expectedValue { 1, 2 }; EXPECT_CALL(foo, SomeMethod(expectedValue));
So, the easiest way to handle this is to define an equality operator for the structure:
struct SomeStruct { int data1; int data2; bool operator==(const SomeStruct& rhs) const { return data1 == rhs.data1 && data2 == rhs.data2; } };
If you do not want to go this route, you can use Field Match to map the parameter based on the values โโof its member variables. (However, if the test is interested in comparing equality between instances of the structure, this is a good sign that other code will be interesting. Therefore, you probably just need to define operator== and end it.)
source share