Can't use SetArgReferee when method doesnt have return type as void

2,410 views
Skip to first unread message

Edison Gustavo Muenz

unread,
Nov 13, 2009, 5:15:08 PM11/13/09
to googl...@googlegroups.com
Hello everyone, consider I have this kind of class to mock:

class ClassToMock {
public:
   virtual bool testMethod(int& a) {
      a= 10;
      return true;
   }
};

so I write:

class MockedA : public ClassToMock
{
public:
   MOCK_METHOD1(testMethod, bool(int&));
};

then I want to write on my tests:

MockedA mock;
EXPECT_CALL(mock, testMethod(_))
        .WillOnce(DoAll(Return(false), SetArgReferee<0>(5)));

Saying that when the 'testMethod' is invoked it will return false and set its arg as 5.

But I'm not allowed to do that, I get lots of compile errors, am I doing something unusual here?

To work around this I usually use the ON_CALL macro to set the value, but that is pretty ugly and error prone, so I would like to embedded my expecations on the same EXPECT_CALL macro

Thanks a lot for your help,
Edison Gustavo Muenz

Zhanyong Wan (λx.x x)

unread,
Nov 13, 2009, 5:45:26 PM11/13/09
to Edison Gustavo Muenz, googl...@googlegroups.com
On Fri, Nov 13, 2009 at 2:15 PM, Edison Gustavo Muenz
<edison...@gmail.com> wrote:
> Hello everyone, consider I have this kind of class to mock:
> class ClassToMock {
> public:
>    virtual bool testMethod(int& a) {
>       a= 10;
>       return true;
>    }
> };
> so I write:
> class MockedA : public ClassToMock
> {
> public:
>    MOCK_METHOD1(testMethod, bool(int&));
> };
> then I want to write on my tests:
> MockedA mock;
> EXPECT_CALL(mock, testMethod(_))
>         .WillOnce(DoAll(Return(false), SetArgReferee<0>(5)));
> Saying that when the 'testMethod' is invoked it will return false and set
> its arg as 5.
> But I'm not allowed to do that, I get lots of compile errors, am I doing
> something unusual here?

You should swap the two arguments to DoAll(). In other words, set the
argument *before* returning false.


> To work around this I usually use the ON_CALL macro to set the value, but
> that is pretty ugly and error prone, so I would like to embedded my
> expecations on the same EXPECT_CALL macro
> Thanks a lot for your help,
> Edison Gustavo Muenz
>



--
Zhanyong

Edison Gustavo Muenz

unread,
Nov 13, 2009, 5:55:18 PM11/13/09
to googl...@googlegroups.com
It worked... I feel embarrassed :)

Anyway, thanks a lot
Reply all
Reply to author
Forward
0 new messages