Can gmock be used for stubbing C functions?

6,123 views
Skip to first unread message

Shan Rehman

unread,
Aug 12, 2015, 11:47:32 AM8/12/15
to Google C++ Mocking Framework
Hello,

I'm new to gmock, so I want to know how can I stub simple C functions called in a function under test.

Example:

int func(int a)
{
   boolean find;
   // Some code
   find = func_to_be_mocked();
   return find;
}

Does gmock provides functionality to mock or stub func_to_be_mocked?

Thanks..

Keith Ray

unread,
Aug 12, 2015, 12:47:53 PM8/12/15
to Shan Rehman, Google C++ Mocking Framework
Change 'func_to_be_mocked' to a global variable pointing to a function object with virtual operator () or make it a function pointer.

I don't recall if virtual operator () is mockable with gtest. 

--

---
You received this message because you are subscribed to the Google Groups "Google C++ Mocking Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googlemock+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/googlemock/49cdf6f0-a547-4f5d-ab16-a7a64243cc84%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Shan Rehman

unread,
Aug 13, 2015, 2:23:11 PM8/13/15
to Google C++ Mocking Framework, shanl...@gmail.com
So basically we cann't stub simple C functions via gmock and its only meant for stubbing or mocking C++ classes methods??

For manual stubbing, the global declaration of 'func_to_be_mocked' should be in gtest code or in source code??
As in unit testing source code shouldn't be changed..

Keith Ray

unread,
Aug 13, 2015, 2:58:51 PM8/13/15
to Shan Rehman, Google C++ Mocking Framework
On 2015 Aug 12, at 10:49 AM, Shan Rehman <shanl...@gmail.com> wrote:

So basically we cann't stub simple C functions via gmock and its only meant for stubbing or mocking C++ classes methods??


Yes.


For manual stubbing, the global declaration of 'func_to_be_mocked' should be in gtest code or in source code??

your test code

As in unit testing source code shouldn't be changed..

That's often impractical. 

It would work better if you use CMock or another C-language-based mocking tool. In order to stub or mock a C function, you have to have your test link against a different implementation of that C function. (Or use function pointers.)


One way of using function-pointers is something like this (pseudo C/C++ code):

Instead of this in the header:

void inner(int x);

use this:

typedef void (*InnerFunc)(int x);
extern InnerFunc inner;

In the implementation of inner:

static void inner_impl(int x) { 
... etc..... 
}

InnerFunc inner = inner_impl;

In the test:

static void inner_mock(int x) { 
... etc..... 
}

...blah blah gmock test stuff....

setup() {
save_inner = inner;
inner = inner_mock;
}

teardown()
{
inner = save_inner;
}

test()
{
set expectations for inner_impl
outer();
assert that expectations for inner_impl were satisfied
}

Syntax of the production code remains the same (though now it is actually calling a function-pointer instead of direct reference to the function.)

void outer(void)
{
      inner(1234);
}


This book explains how you can do TDD and mocking in C:



Shan Rehman

unread,
Aug 18, 2015, 10:18:48 AM8/18/15
to Google C++ Mocking Framework
It's a good example to implement stubbing via function pointer however I don't understand the part where 
you have written 'In the implementation of inner:'. Is this in source code file? If yes, then  'inner' function 
definition will be changed to 'inner_impl' in source code. 
Reply all
Reply to author
Forward
0 new messages