I'd just use WithArg
and Invoke
with a lambda rather than ReturnNew
in this case:
ON_CALL(mocked_object, MockedMethod(_))
.WillByDefault(WithArg<0>(Invoke(
[&mocked_object](ObjectB* b) { return new ObjectA(&mocked_object, b); })));
Frage
How can I mock a method that returns a new object whose constructor takes one of the method's arguments as an argument of its own?
// ObjectA::ObjectA(MockedObject arg1, ObjectB* arg2);
// ObjectA* MockedObject::MockedMethod(ObjectB* arg);
ON_CALL(mocked_object, MockedMethod(_))
.WillByDefault(ReturnNew<ObjectA>(mocked_object, new ObjectB()));
In the above example, rather than using new ObjectB()
as the argument for ReturnNew<ObjectA>()
, I would like to use the _
argument originally passed to MockedMethod
.