Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function mocking in C?

I'm writing a unit-test to check some API calls. I am using check to test. My module is build with CMake (idk if it matters).

My test calls a function (which I need to test) and this function makes a call to another binary.

Simplified version of it looks like this.

/* unitTest.c */

#include "libraryAPI.h"
void letsMakeACall(void)
{
   ck_assert_eq(foo("water"), 0);
}

-- Module I am working on---
/*libraryAPI.c*/
#include "legacyLib.h"

void foo(const char *drink )    
{

    if (checkDrink(drink)!=0)
    {
       return 1;
    }else
    {
       return 0;
    }
}


----LEGACY BINARY---
/*legacyLib.c*/

static const char* expected = "water";

void checkDrink(const char *drink)
{
    if(drink == expected)
     {
        /*There are also a dozen functions being called which depend on legacy module initialisation*/
        return 0;
     }else{
        return 1;
     }
}

I'd like to mock response from legacyLib, because otherwise it call dozens of functions and breaks. My initial idea was to add some ifdef conditions when tests are being run, but it is against guidelines. Because it is basically a call interception I don't know what it a best(or working) solution. What can I use to solve it?

like image 248
Oreols Avatar asked Jul 30 '16 19:07

Oreols


People also ask

What is a mocked function?

Mock functions allow you to test the links between code by erasing the actual implementation of a function, capturing calls to the function (and the parameters passed in those calls), capturing instances of constructor functions when instantiated with new , and allowing test-time configuration of return values.

How do you use mock function?

You can mock functions in two ways: either you create a mock function to use in test code, or you write a manual mock that overrides a module dependency. Let's take for example the case where we're testing an implementation of a function forEach, which will invoke a callback for each item in a supplied array.

What is a stub vs mock?

Stub: a dummy piece of code that lets the test run, but you don't care what happens to it. Substitutes for real working code. Mock: a dummy piece of code that you verify is called correctly as part of the test. Substitutes for real working code.

What is mock function in unit testing?

unittest.mock is a library for testing in Python. It allows you to replace parts of your system under test with mock objects and make assertions about how they have been used. unittest.mock provides a core Mock class removing the need to create a host of stubs throughout your test suite.


1 Answers

I am also unsure how to solve this generally, I have posted a similar question, but in some cases you can do the following (presuming you are testing individual functions):

  1. Include the .c file instead of the header .h, but after you "rename" your mocked function using a define directive:

    #define checkDrink checkDrink_mocked
    // preprocessor will now replace all occurrences of "checkDrink"
    // with "checkDrink_mocked"
    
    int checkDrink_mocked(const char *drink); 
    #include "legacyLib.c"
    #undef checkDrink
    
  2. Implement the renamed function:

    int checkDrink_mocked(const char *drink)
    {
        return 15;  
    }
    
like image 112
Lou Avatar answered Sep 20 '22 12:09

Lou