Problem with "WithArg" and SetArgReferee, cannot deduce template type

2,522 views
Skip to first unread message

Edison Gustavo Muenz

unread,
Dec 1, 2009, 8:24:35 AM12/1/09
to Google C++ Mocking Framework
Hello all, I'm having a little bit of problem using WithArg and SetArgReferee together

suppose I have this class:

class MyMock {
public:
   MOCK_METHOD2(DoIt, void(int p0, int& p1));
};

And I want to make calls to "DoIt" assign: 'p1 = p0'

I looked at the documentation and found the two functions WithArg  and  SetArgReferee, so I tried:

void test()
{
   using namespace testing;

   MyMock mock;
   ON_CALL(mock, DoIt(_, _))
      .WillByDefault(WithArg<0>(SetArgReferee<1>));
}

But I wasnt successfull, the error message I get is this:

1>d:\framework_app\am_tests\helpers\doccadmoldesbuilder.cpp(76) : error C2780: 'testing::SetArgRefereeActionP<k,value_type> testing::SetArgReferee(value_type)' : expects 1 arguments - 1 provided
1>        d:\framework_app\third_part\gmock-1.4.0\include\gmock\gmock-more-actions.h(144) : see declaration of 'testing::SetArgReferee'
1>d:\framework_app\am_tests\helpers\doccadmoldesbuilder.cpp(76) : error C2784: 'testing::internal::WithArgsAction<InnerAction,k1> testing::WithArg(const InnerAction &)' : could not deduce template argument for 'overloaded function type' from 'overloaded function type'
1>        d:\framework_app\third_part\gmock-1.4.0\include\gmock\gmock-more-actions.h(121) : see declaration of 'testing::WithArg'

Since SetArgReferee is an Action, I thought I would be able to do this kind of binding on the args. Where am I missing the point here?

Thanks for any help

--
Edison Gustavo Muenz

Zhanyong Wan (λx.x x)

unread,
Dec 1, 2009, 12:00:52 PM12/1/09
to Edison Gustavo Muenz, Google C++ Mocking Framework
SetArgReferee is not an Action. Neither is SetArgReferee<1>.

http://code.google.com/p/googlemock/wiki/CheatSheet#Side_Effects

SetArgReferee<1>(value) is an Action. It requires you to know the
value when you write the expectation. The value cannot be obtained
from a mock function argument.

What you need is a custom action:

http://code.google.com/p/googlemock/wiki/CheatSheet#Defining_Actions

ACTION(AssignArg0ToArg1) { arg1 = arg0; }
...WillByDefault(AssignArg0ToArg1());

For more details,
http://code.google.com/p/googlemock/wiki/CookBook#Writing_New_Actions_Quickly

> Thanks for any help
> --
> Edison Gustavo Muenz
>



--
Zhanyong
Reply all
Reply to author
Forward
0 new messages