Search code examples
matchergooglemockoutput-parameter

googlemock matcher for output parameters


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?


Solution

  • 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);