Invoke a function on a passed argument

10,905 views
Skip to first unread message

mrp

unread,
Jan 13, 2017, 11:57:42 AM1/13/17
to Google C++ Mocking Framework


EXPECT_CALL(_http, post(testing::_, testing::_, testing::_)).WillOnce(testing::Invoke(Argument<2>.complete(200, ""));

My idea is to invoke a function complete(int, string) on the 3rd argument passed in post call. 

Could anyone help?

Samuel Benzaquen

unread,
Jan 13, 2017, 3:06:17 PM1/13/17
to mrp, Google C++ Mocking Framework
The simplest solution is to use a lambda in the Invoke call.

EXPECT_CALL(_http, post(testing::_, testing::_, testing::_)).WillOnce(testing::Invoke(
    [] (Arg1 arg1, Arg2 arg2, Arg3 arg3) { arg2.complete(200, ""); }));
 
When the builtin actions don't do what you need, a lambda is a good escape hatch.

--

---
You received this message because you are subscribed to the Google Groups "Google C++ Mocking Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googlemock+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/googlemock/929176b3-7996-4d2d-8e07-6873c330ef01%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mrp

unread,
Jan 17, 2017, 2:12:16 PM1/17/17
to Google C++ Mocking Framework, meerarp...@gmail.com
Thanks Samuel.

I modified my EXPECT_CALL as :

EXPECT_CALL(_http, post(testing::_, testing::_, testing::_))
   .WillOnce(testing::WithArg<2>(testing::Invoke(
[] (const std::shared_ptr <HttpCallback> &callback)
{
callback->complete(200, "");
})));

and it worked.
Reply all
Reply to author
Forward
0 new messages