Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Mock: How to configure custom message to explain match failure

Tags:

c++

googlemock

In case of match failure Google mock prints message like the following:

test.cpp:112: EXPECT_CALL(mock_obj, foo( MyMatcher( bar ) ))...
Expected arg #0: is equal to [1,2; 3,4]
Actual: { 1 }
Expected: to be called once
Actual: never called - unsatisfied and active

Using custom matcher MyMatcher I'm able to define a description string which is used to generate the failure message when the match fails. But it defines only Expected arg #0 part of message. Is there any way how I can customize how Actual is printed?

In my case I can't overload operator<< for class of bar as it already overloaded by third party code is not under my control (this library define class of bar and operator<< for it).

like image 921
Alexander Gorban Avatar asked Mar 11 '13 21:03

Alexander Gorban


1 Answers

As I'm not able to define operator << for my class, google test have option to define PrintTo function for it Teaching Google Test How to Print Your Values. Thanks RA for the link.

A user can override this behavior for a class type Foo by defining an overload of PrintTo() in the namespace where Foo is defined. We give the user this option as sometimes defining a << operator for Foo is not desirable (e.g. the coding style may prevent doing it, or there is already a << operator but it doesn't do what the user wants).

void PrintTo(const Bar& value, ::std::ostream* os);
like image 188
Alexander Gorban Avatar answered Sep 17 '22 22:09

Alexander Gorban