Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GoogleTest how to use InvokeArgument With WithArg

I have a mock function:

MOCK_METHOD4(my_func, int(double, double, void* (*cb) (int), int p1));

I want to invoke 2nd (0-based) argument of above function with the 3rd argument as parameter, i.e., invoke "cb" function with "p1" as parameter. How can I do that?

I can invoke "cb" with some custom value using InvokeArgument:

ON_CALL(mockObj, my_func(_, _, _, _)).
                WillByDefault(DoAll(
                        IgnoreResult(InvokeArgument<2>(10)),
                        Return(0)));

But I want to invoke it with an actual parameter passed to the same mocked function call.

like image 737
usman Avatar asked Jul 07 '16 07:07

usman


People also ask

What is expect_ CALL in gmock?

EXPECT_CALL not only defines the behavior, but also sets an expectation that the method will be called with the given arguments, for the given number of times (and in the given order when you specify the order too).

How do you mock in Gmock?

first, you use some simple macros to describe the interface you want to mock, and they will expand to the implementation of your mock class; next, you create some mock objects and specify its expectations and behavior using an intuitive syntax; then you exercise code that uses the mock objects.


1 Answers

You can define an ACTION to invoke your callback. Something like below:

ACTION(CallCb) {
  arg2(arg3);
}

...

ON_CALL(*mockObj, my_func(_, _, _, _))
  .WillByDefault(
     DoAll(CallCb(),
           Return(0)));
like image 75
Mine Avatar answered Sep 24 '22 00:09

Mine