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

Categories

HOME
turing-machines
error-handling
grafana
newrelic
cublas
square
nested-if
limit
hpc
nfs
rcloud
sendgrid-api-v3
mod-security
cmdb
jxl
shared-libraries
symbol
scsi
searchview
null-pointer
smartclient
jmp
kendo-mobile
href
beta
expressionengine3
surveymonkey
fat
pylons
mat
fractions
rpgle
finagle
constexpr
boost-asio
scrollview
turfjs
baidu-map
sharepoint-workflow
zoomify
openalpr
comparison-operators
apollostack
memory-mapping
optimizely
x++
gitlist
identification
carrot2
tinkerpop
trigger.io
model-associations
gogs
clipboard.js
nservicebus5
php-socket
query-by-example
realsense
custom-errors
sapi
linklabel
ess
dock
baasbox
skspritenode
spy++
rmysql
koala
hibernate-entitymanager
jammer
time.h
twisted.web
tree-balancing
mkv
subproject
lambdaj
stress
filemerge
android-holo-everywhere
http-daemon
photolibrary
resharper-6.0
blender-2.49
spread
escrow
unmanagedresources
technical-debt
business-model
wcf-proxy

Resources

Encrypt Message