在不编辑生产代码的情况下在 C++ 中模拟非虚拟方法?

Mocking non-virtual methods in C++ without editing production code?(在不编辑生产代码的情况下在 C++ 中模拟非虚拟方法?)
本文介绍了在不编辑生产代码的情况下在 C++ 中模拟非虚拟方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名相当新的软件开发人员,目前正在为多年前开始的现有 C++ 项目添加单元测试.由于非技术原因,我不允许修改任何现有代码.我所有模块的基类都有一堆设置/获取数据和与其他模块通信的方法.

I am a fairly new software developer currently working adding unit tests to an existing C++ project that started years ago. Due to a non-technical reason, I'm not allowed to modify any existing code. The base class of all my modules has a bunch of methods for Setting/Getting data and communicating with other modules.

由于我只想对每个单独的模块进行单元测试,因此我希望能够为我的所有模块间通信方法使用固定值.IE.对于检查另一个模块是否处于活动状态的 Ping() 方法,我希望它根据我正在做的测试类型返回 true 或 false.我一直在研究 Google Test 和 Google Mock,它确实支持模拟非虚拟方法.但是所描述的方法(http://code.google.com/p/googlemock/wiki/CookBook#Mocking_Nonvirtual_Methods)要求我将原始方法模板化"以获取真实或模拟对象.由于前面提到的要求,我不能在基类中模板化我的方法,所以我需要一些其他方法来模拟这些虚拟方法

Since I just want to unit testing each individual module, I want to be able to use canned values for all my inter-module communication methods. I.e. for a method Ping() which checks if another module is active, I want to have it return true or false based on what kind of test I'm doing. I've been looking into Google Test and Google Mock, and it does support mocking non-virtual methods. However the approach described (http://code.google.com/p/googlemock/wiki/CookBook#Mocking_Nonvirtual_Methods) requires me to "templatize" the original methods to take in either real or mock objects. I can't go and templatize my methods in the base class due to the requirement mentioned earlier, so I need some other way of mocking these virtual methods

基本上,我要模拟的方法在某个基类中,我要对其进行单元测试和创建模拟的模块是该基类的派生类.在我的基本 Module 类和我要测试的模块之间有中间模块.

Basically, the methods I want to mock are in some base class, the modules I want to unit test and create mocks of are derived classes of that base class. There are intermediate modules in between my base Module class and the modules that I want to test.

如果有任何建议,我将不胜感激!

I would appreciate any advise!

谢谢,

JW

更具体的例子

我的基类是rootModule,我要测试的模块是leafModule.有一个继承自rootModule的中间模块,leafModule继承自这个中间模块.

My base class is lets say rootModule, the module I want to test is leafModule. There is an intermediate module which inherits from rootModule, leafModule inherits from this intermediate module.

在我的leafModule 中,我想测试doStuff() 方法,该方法调用rootModule 类中定义的非虚拟GetStatus(moduleName).我需要以某种方式使 GetStatus() 返回一个选定的罐头值.模拟对我来说是新事物,所以使用模拟对象是否是正确的方法?

In my leafModule, I want to test the doStuff() method, which calls the non virtual GetStatus(moduleName) defined in the rootModule class. I need to somehow make GetStatus() to return a chosen canned value. Mocking is new to me, so is using mock objects even the right approach?

推荐答案

我会编写一个 Perl/Ruby/Python 脚本来读取原始源代码树并在不同的目录中写出一个模拟的源代码树.您不必完全解析 C++ 来替换函数定义.

I would write a Perl/Ruby/Python script to read in the original source tree and write out a mocked source tree in a different directory. You don't have to fully parse C++ in order to replace a function definition.

这篇关于在不编辑生产代码的情况下在 C++ 中模拟非虚拟方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)