Reputation: 6186
Say I'm mocking class IClass
which has a method call RClass DoIt(int x)
like this:
class RClass
{
int _x;
public:
RClass(int x) :_x(x) { }
}
class MockClass : public IClass
{
public:
MOCK_METHOD1(DoIt, RClass(int)));
}
Then in my test I want to return a RClass
value constructed with the first argument called in the code under test. I tried like this, but it didn't work:
int value = 0;
MockClass mc;
EXPECT_CALL(mc, DoIt(_)).WillRepeatedly(DoAll(SaveArg<0>(&value), Return(RClass(value))));
Any ideas?
Upvotes: 1
Views: 961
Reputation: 872
Check out the Invoke() action. It allows you to specify arbitrary behavior for a mock method. There are a number of variations, but one form will look something like this:
RClass fake(int x) { return RClass(x); }
ON_CALL(mc, DoIt(_))
.WillByDefault(Invoke(&fake));
If you're the C++11 type, lambdas also work:
ON_CALL(mc, DoIt(_))
.WillByDefault(Invoke([](int x) -> RClass { return RClass(x) }));
Upvotes: 2