gmock
How to call function pointer which is passed to Mock method?
virtual void doSomething(std::function<void(std::error_code result)> callback) = 0 MockMethod1(doSomething, std::function<void(std::error_code result)> callback); Now when I write EXPECT_CALL, how can I call "callback" EXPECT_CALL(object, doSomething(testing::_)).WillRepeatedly( ??? ); What should I write at to call my callback?
No need to do custom action. I found answer as different way, here is what you need to do, EXPECT_CALL(.....).WillRepeatedly(testing::InvokeArgument<1>(arg1, arg2, ..));
Use custom actions to achieve this. To achieve more flexibility, I suggest creating a template for an action, where template parameter signifies argument index of the callback function in doSomething. Here is how you do this: ACTION_TEMPLATE(ExecuteCallback, HAS_1_TEMPLATE_PARAMS(unsigned, uIndex), AND_1_VALUE_PARAMS(callbackArg)) { (std::get<uIndex>(args))(callbackArg); } Note that you can add more value parameters if needed. Now, in you test you call setup mock calls like this: EXPECT_CALL(object, doSomething(testing::_)).WillRepeatedly(DoAll(ExecuteCallback<0>(err), Return()));
Related Links
gmock ReturnRef returns compilation error
How to call function pointer which is passed to Mock method?
In gmock, Is there a way to have mock object return an instance of an user defined object?
GMock testing destructor calls
set EXPECT_CALL to redirect the call to the original method
Capture GMOCK string parameter
Mocking non-virtual method generating errors
gmock matcher doesn't match my arguments by reference
What did you do to solve gmock you mentioned (link enclosed)?
how to set custom ref-variable in gmock