how to mock macro function using google mock

ratzip

I need to mock macro defined in linux header:

#include <asm/types.h>
#include <linux/netlink.h>

for (msg_ptr = (struct nlmsghdr *) reply; 
     NLMSG_OK(msg_ptr, len); 
     msg_ptr = NLMSG_NEXT(msg_ptr, len))
{
   // do something
}

NLMSG_OK is defined in some linux header linux/netlink.h.

How to mock it using google mock?

Does google mock support mocking macro?

πάντα ῥεῖ

How to mock it using google mock?

You cannot.

Does google mock support mocking macro?

No.

First of all, macros aren't functions! They're just text processing templates that are expanded from the preprocessing phase.

OK, let's assume the macro somehow expands to a global function call, they've given the following answer in their FAQ:

My code calls a static/global function. Can I mock it?

You can, but you need to make some changes.

In general, if you find yourself needing to mock a static function, it's a sign that your modules are too tightly coupled (and less flexible, less reusable, less testable, etc). You are probably better off defining a small interface and call the function through that interface, which then can be easily mocked. It's a bit of work initially, but usually pays for itself quickly.

This Google Testing Blog post says it excellently. Check it out


As for your comment "what you mean by using #undef and re #define?":

I meant you need to compile your code under test with your own macro definition, that injects a Google Mock class/function instead.

#if defined(UNDER_TEST)
#undef NLMSG_OK
#define NLMSG_OK(msg_ptr, len) \
     // your mocking code
#endif

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to mock macro function using google mock

From Dev

How to mock a tornado coroutine function using mock framework for unit testing?

From Dev

How to mock Google Analytics function call ga()

From Dev

Mock method implementation using Google Mock

From Dev

How to mock a decorated function

From Dev

Google Mock - how to name mock functions?

From Dev

How to mock method with optional parameter in Google Mock?

From Dev

How to mock method with optional parameter in Google Mock?

From Dev

Invoking Overloaded Function on Google Mock

From Dev

Using google mock for C code

From Dev

Google Mock and SetArgPointee using Objects

From Dev

google mock - how to expect function to NOT be called with set of parameters

From Dev

How to mock a class with both virtual and non-virtual methods using Google Mock?

From Dev

how to mock failure for google oauth?

From Dev

How to mock jquery ready function

From Dev

Python – How to mock a single function

From Dev

How to mock nested function in Jest?

From Dev

How to stub/mock a const function

From Dev

Python – How to mock a single function

From Dev

Kotlin how to mock function in parameters

From Dev

How to check if a function was called in a unit test using pytest-mock?

From Dev

How to mock a function using rewirejs and chai-spies in order to test it?

From Dev

cannot mock CDatabase Open/OpenEx using google mock c++

From Dev

cannot mock CDatabase Open/OpenEx using google mock c++

From Dev

How to mock a For Loop using Mockito

From Dev

How to mock Context using Mockito?

From Dev

How to mock a SharedPreferences using Mockito

From Dev

how to mock QueueItems[] using mockito?

From Dev

How to mock this JSONObject using PowerMockito?

Related Related

HotTag

Archive