Can gmock create mock functions for Global functions?

5,222 views
Skip to first unread message

sarbojit

unread,
Aug 25, 2011, 12:37:59 PM8/25/11
to Google C++ Mocking Framework
Hi,
I was looking into cook book but did not find a way to mock global
functions(c type functions). There they have advised to wrap it into a
class and then use but I was looking for some better solution.

Please let me know if it is possible.

Sarbojit Sarkar

unread,
Aug 26, 2011, 1:07:57 AM8/26/11
to Google C++ Mocking Framework
I believe google must have thought about it.
 
One more requirement I am having like,
I am having list of function pointers which I wanted to assigne mock functions generated but gmock, is it possible? Currently I am getting compilation error. not sure if I am trying it in a correct way. e.g.
 
Init_func()
{
.......
MOCK_METHOD2(func,int(int a, char c, float f));
 
int (*foo)(int, char, float);
 
foo = func;
........
}
 
Please let me know the correct way if it is not a correct way.

Mike Long

unread,
Aug 26, 2011, 3:42:58 AM8/26/11
to Sarbojit Sarkar, Google C++ Mocking Framework
Googlemock does not support mocking free/global functions:

You can follow the steps in this blog post to change your code to be mockable:

However, if you do not wish to hide access to the function through an interface, you might consider taking a look at my fake function framework.  It works perfectly well alongside googletest and googlemock:

Regards,
Mike
Reply all
Reply to author
Forward
0 new messages