4

I am testing whether my class calls a method on a mocked class, with the proper argument. I have set up a basic expectation:

// mListener is a mocked object
// This expectation accepts any argument
EXPECT_CALL(this->mListener, OnChanged(_))
    .Times(1);

This is fine, but I also want to verify the argument. It is an object which only has accessors that use output parameters:

// aValue is an output parameter
HRESULT get_Value(int* aValue);

How can I define a matcher that will inspect the value that get_Value puts into aValue?

sourcenouveau
  • 29,356
  • 35
  • 146
  • 243

1 Answers1

4

You could try something like:

MATCHER_P(CheckValue,
          expected_value,
          std::string("get_Value ")
              + (negation ? "yields " : "doesn't yield ")
              + PrintToString(expected_value)
              + " as expected.") {
  int result;
  arg.get_Value(&result);
  return expected_value == result;
}

which could check that e.g. aValue == 7 by doing:

EXPECT_CALL(this->mListener, OnChanged(CheckValue(7)))
    .Times(1);
Fraser
  • 74,704
  • 20
  • 238
  • 215
  • Perfect, thank you! The googlemock cookbook page has information on custom matchers: http://code.google.com/p/googlemock/wiki/CookBook#Writing_New_Parameterized_Matchers_Quickly – sourcenouveau Feb 23 '12 at 14:17
  • Also I added some custom printing code so that when the test fails I get some numbers, rather than the first few bytes of my object. http://code.google.com/p/googletest/wiki/AdvancedGuide#Teaching_Google_Test_How_to_Print_Your_Values – sourcenouveau Feb 23 '12 at 16:32