How to mock library calls?

New for cpp (Java guy).

I have a third-party library that has a sendMail (txt) method. I do not want to check the library. I want to test my own method, so for this I need to make fun of library calls.

My own method is as follows:

#include "mailsender.h" int run(txt){ analysis(txt); ... ... int status = sendMail(txt);//sendMail is a 3rd party library call. i need to mock it.its not part of the unit test return status; } 

In Java, the mail agent was an interface and it was injected into my class, so in the case of a test, I add a layout. What is good practice in cpp for bullying library calls? I can wrap a call to a third-party library in a class and introduce this class, but I'm looking for something simpler for normal practice (possibly ifndf).

I am familiar with googlemock. googlemock allows me to make fun of classes. I don’t know how to mock a call in my tested method.

+4
source share
3 answers

So, I assume that you have a global function that is implemented in the library, in which you both include the header file (to get the definition) and the link (to get the implementation).

You obviously need to replace the library implementation with your own - one that does nothing, so you can do this in two ways:

  • you replace .dll (or .so) with your own implementation, which has all the methods provided by a third-party library. This is easy once you have written a new version of all third-party lib functions, but writing them all can be a pain.
  • you temporarily delete the library and replace the calls you make with the original .cpp file that implements these functions. Thus, you will create your own sendMail () function in the .cpp file and include it in the program instead of mailsender.h.

The latter is simpler, but you may also need to modify your program so as not to mess with a third party lib. This may also require a #include change, as some compilers (such as VC ++) allow you to embed linker directives in your source code. If you do this, you cannot stop the linker from including a third-party library.

Another option is to change the code to use another call to sendMail, for example test__sendMail (), which you implement yourself. Wrap this macro to conditionally enable your or actual function call depending on your build options.

If it was a C ++ library, then you can probably use the fake infrastructure, as you are used to, but it looks like its C library, and they just provide a list of functions that you use directly in your code. You can wrap the library in your own class and use this, and not directly reference third-party lib functions.

There is a list of C mocking frameworks.

+3
source

This is an old question with an answer already selected, but maybe the following contribution might help someone else.

First decision

You still need to create a custom library to override functions, but you do not need to change the Makefiles links to your "fake-library", just use LD_PRELOAD with the path of the fake library and that will be the first that the linker will find and then will use.

example

Second solution

ld (GNU) The linker has the --wrap option , which allows you to wrap only one function with another provided by the user . This way you don't need to create a new library / class to mock behavior

Here is an example from the man page

- wrap = character Use the wrapper function for the character. Any undefined symbol references will be resolved to "__wrap_ symbol". Any undefined reference to the "__real_" character will resolve the character.

This can be used to provide a wrapper for a system function. The wrapper function should be called the __wrap_ symbol. If he wishes to call the system function, he must call the __real_ character.

Here is a trivial example:

 void * __wrap_malloc (size_t c) { printf ("malloc called with %zu\n", c); return __real_malloc (c); } 

If you link other code to this file using --wrap malloc, then all calls to malloc will call the __wrap_malloc function. Calling __real_malloc in __wrap_malloc will call the real function malloc.

You might also want to provide the __real_malloc function, so links without the --wrap option will succeed. If you do this, you should not put the definition of __real_malloc in the same file as __wrap_malloc; if you do this, assembler can resolve the call until the linker has the option to wrap it in "malloc".

+2
source

Although there is no interface keyword, you can use abstract base classes for similar things in C ++.

If the library you are using does not contain such abstractions, you can wrap it in your own β€œinterface”. If your code separates building objects from use (for example, using IoC), you can either use it to fake it or use Mocks:

https://stackoverflow.com/questions/38493/are-there-any-good-c-mock-object-frameworks

0
source

All Articles