Call function as action with custom arguments

2,375 views
Skip to first unread message

MartinS

unread,
Sep 16, 2011, 3:20:22 AM9/16/11
to googl...@googlegroups.com
Hi,

This (pseudocode) is something I find myself wanting to do, almost every day, but fail to find a _simple_ way to do:

EXPECT_CALL(mock, method(_, _))
.WillOnce(InvokeWithCustomArgs(function("hello", 3)));

i.e. invoke a function, but use my _own_ arguments, not the arguments from the mock call. Even better, mixing mock arguments and custom arguments:

EXPECT_CALL(mock, method(_, _))
.WillOnce(InvokeWithCustomArgs(function(Arg <1> (), 3)));

Kindly
/Martin

arsenal

unread,
Sep 16, 2011, 3:48:16 AM9/16/11
to Google C++ Mocking Framework
I am not sure about InvokeWithCustomArgs() but ACTION_P2(action,
params) will solve the problem in this case.

simply define this,

ACTION_P2(SimpleFunction, a, b)
{
// your logic in this function. SimpleFunction() will have access to
all the function arguments along with your custom arguments (a, b)
}

and use that action while expecting a call.

EXPECT_CALL(mock, method(_, _))
.WillOnce(SimpleFunction("hello", 3)));

hope that helps.

MartinS

unread,
Sep 16, 2011, 4:10:16 AM9/16/11
to googl...@googlegroups.com
Yeah, thats the way I've found, but it is a bit cumbersome since I end up with hundreds of these.
I hoped there would be an easier way. (Such as the imaginary "InvokeWithCustomArgs" action.)

Vlad Losev

unread,
Sep 16, 2011, 6:53:40 AM9/16/11
to googl...@googlegroups.com
The Invoke action can accept functors. That means you can use boost::bind to pass whatever arguments you want into your function.

Martin Svensson

unread,
Sep 16, 2011, 2:16:02 PM9/16/11
to googl...@googlegroups.com
That sounds interesting. I'll check it out.

/Martin

Reply all
Reply to author
Forward
0 new messages