How to mock out reference parameter

11,190 views
Skip to first unread message

zilong

unread,
Feb 20, 2012, 6:41:05 AM2/20/12
to Google C++ Mocking Framework
Hi all,

class IFoo {
virtual void get_name(std::string& name) = 0;
};

class MockIFoo : public IFoo {
MOCK_METHOD1(get_name, void(std::string& name));
};

When I use this mock object in test codes, how could I set the
expectations to get out parameter as I specified, I tried this way,
but not work.
std::string dummy("foo");
EXPECT_CALL(foo, get_name(dummy));


Best regards,
Zilong

Greg Miller

unread,
Feb 21, 2012, 10:09:07 AM2/21/12
to zilong, Google C++ Mocking Framework
Hi, Zilong.

You should be able to use the SetArgReferee() action.

  MockIFoo foo;
  EXPECT_CALL(foo, get_name(_))
      .WillOnce(testing::SetArgReferee<0>("dummy"));
  string dummy;
  foo.get_name(dummy);
  ASSERT_EQ("dummy", dummy);

HTH,

Greg

zilong

unread,
Feb 21, 2012, 10:27:47 AM2/21/12
to Google C++ Mocking Framework
Cool, it works now, many thanks, Greg.


Best regards,
Zilong
Reply all
Reply to author
Forward
0 new messages