在不编辑生产代码的情况下使用C ++模拟非虚拟方法? [英] Mocking non-virtual methods in C++ without editing production code?

查看:89
本文介绍了在不编辑生产代码的情况下使用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.

由于我只想对每个单独的模块进行单元测试,因此我希望能够为所有的模块间通信方法使用罐头值。也就是说对于一个方法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

基本上,方法我想mock是在一些基类,我想要的模块单元测试和创建mocks是派生类的基类。在我的基本模块类和我想测试的模块之间有中间模块。

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.

我很感激任何建议!

感谢,

JW

>

A more concrete examples

我的基类是让我们说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()方法,虚拟GetStatus(moduleName)在rootModule类中定义。我需要以某种方式让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 ++模拟非虚拟方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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