如何模拟库调用? [英] How to mock library calls?

查看:82
本文介绍了如何模拟库调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cpp的新手(Java专家)。

New to cpp (Java guy).

我有3rd方库,该库具有方法sendMail(txt)。
我不想测试库。我想测试自己的方法,因此,为了做到这一点,我需要模拟库调用。

I have 3rd party library that has method sendMail(txt). I don't want to test the library. i want to test my own method, so in order to do this , i need to mock the library calls .

我自己的方法如下:

#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;
}

在Java中,mailsender是接口,并且已注入到我的类中,因此在测试的情况下我注入模拟。
在cpp中模拟库调用的良好实践是什么?
我可以将3rd party库调用包装在一个类中并注入该类,但是我正在寻找更简单且更通用的方法(也许是ifndf)。

In Java the mailsender was interface and it was injected to my class, so in case of test i inject mock. What is a good practice in cpp to mock library calls? I can wrap the 3rd party library call in a class and inject this class, but i am looking for something simpler and for the common practice (maybe ifndf).

我对googlemock很熟悉。
googlemock允许我模拟类。我不知道该如何在测试的方法中模拟呼叫。

I am familiar with googlemock. googlemock allow me to mock classes . i am not aware to option how to mock a call in my tested method.

推荐答案

所以我假设您有一个'global'在库中实现的函数,您都需要包含一个头文件(以获取定义)和链接(以获取实现)。

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

您显然需要替换用您自己的库的实现-一种不执行任何操作的库,因此您可以通过2种方法来实现:

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


  • 您替换了.dll(或.so)以及您自己的实现,该实现具有第三方库公开的所有方法。一旦编写了所有3rd party lib函数的新版本,这很容易,但是全部写出来可能很麻烦。

  • 您临时删除该库,并替换调用您可以在实现这些功能的.cpp源文件中进行说明。因此,您需要在.cpp文件中创建自己的sendMail()函数,并将其包含在程序中,而不是mailsender.h include中。

后者更容易,但是您可能还必须修改程序以不与第三方库链接。这也可能还需要更改#include,因为某些编译器(例如VC ++)允许您将链接器指令嵌入源代码中。如果这样做,则将无法阻止链接程序包含第三方库。

The latter is easier, but you might also have to modify your program to not link with the 3rd party lib. This can also require changing the #include as well, as some compilers (eg VC++) allow you to embed linker directives in the source. If your does this, then you won't be able to stop the linker from including the 3rd party lib.

另一种选择是修改代码以使用其他代码。调用sendMail调用,例如您自己实现的test__sendMail()。包装这是一个宏,可以根据您的构建选项有条件地包含您的调用或真正的函数调用。

The other option is to modify your code to use a different call to the sendMail call, eg test__sendMail() that you implement yourself. Wrap this is a macro to conditionally include your, or the real, function call depending on your build options.

如果这是一个c ++库,那么您也许可以可以像以前一样使用模拟框架,但这听起来像是C库,它们只是提供了直接在代码中使用的函数列表。您可以将库包装在自己的类中,并使用它,而不是直接调用第三方库函数。

If this was a c++ library then you'd probably be able to use a mocking framework like you're used to, but it sounds like its a C library, and they simply provide a list of functions that you use directly in your code. You could wrap the library in your own class and use that instead of calling the 3rd party lib functions directly.

这里有一个C模拟框架清单。

这篇关于如何模拟库调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆