Mocking non-virtual methods in C++ without editing production code?(在不编辑生产代码的情况下在 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++ 中模拟非虚拟方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在不编辑生产代码的情况下在 C++ 中模拟非虚拟方法?
基础教程推荐
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- C++,'if' 表达式中的变量声明 2021-01-01