Matching user type arguments in googlemock

I have a problem with matching a function argument to a specific object using google mock.

Consider the following code:

class Foo { public: struct Bar { int foobar; } void myMethod(const Bar& bar); } 

Now I have the code for testing, it might look like this:

 Foo::Bar bar; EXPECT_CALL(fooMock, myMethod(Eq(bar)); 

So I want to make sure that when Foo :: myMethod is called, the argument looks like my locally based object.

When I try to use this approach, I get an error message like:

 gmock/gmock-matchers.h(738): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const Foo::Bar' (or there is no acceptable conversion) 

I tried playing with the defining operator == and! = (At least as a member of a free function) using Eq (ByRef (bar)), but I could not fix this problem. The only thing that helps is to use

 Field(&Foo::Bar::foobar, x) 

but so I have to check every field in my structure, which seems to print a lot of work ...

+5
source share
1 answer

OK, then I will answer to myself:

You must provide the operator == implementation for Foo :: Bar:

 bool operator==(const Foo::Bar& first, const Foo::Bar& second) { ... } 

Do not add it as a member function in Foo :: Bar, but use a free function.

And, with lessons learned, be careful not to put them in an anonymous namespace.

+5
source

Source: https://habr.com/ru/post/1487446/


All Articles