假/非虚拟C ++方法 [英] fake/mock nonvirtual C++ methods

查看:129
本文介绍了假/非虚拟C ++方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它知道在C + +嘲笑/伪造非虚拟方法测试是很困难的。例如, googlemock 食谱有两个建议 - 这意味着以某种方式修改原始的源代码(模板并重写为接口)。

It known that in C++ mocking/faking nonvirtual methods for testing is hard. For example, cookbook of googlemock has two suggestion - both mean to modify original source code in some way (templating and rewriting as interface).

这看起来对于C ++代码来说是非常糟糕的问题。如果你不能修改需要伪造/嘲笑的原始代码,如何做到最好?复制整个代码/类(使用整个基类层次结构)

It appear this is very bad problem for C++ code. How can be done best if you can't modify original code that needs to be faked/mocked? Duplicating whole code/class (with it whole base class hierarchy??)

推荐答案

我们有时使用的一种方法是原来的.cpp文件至少分为两部分。

One way that we sometimes use is to split the original .cpp file into at least two parts.

然后测试设备可以提供自己的实现;有效地使用链接器为我们做脏工作。

Then the test apparatus can supply its own implementations; effectively using the linker to do the dirty work for us.

这被称为 Link Seam

这篇关于假/非虚拟C ++方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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