mock non-virtual methods
生产代码中有很多类方法是非虚的,而为了在Gtest中解除这些非必需的依赖,可以通过Gmock的mock non-virtual methods using templates方法来达到目的。
在此之前,需要了解一种设计模式:Dependency Injection,依赖注入。虽然这个概念始于Java和.net,但在面向对象编程中,C++代码同样应该遵循。
Ps:软件工程中的一个重要的理念就是关注分离(Separation of concern, SoC)。依赖注入不是目的,它是一系列工具和手段,最终的目的是帮助我们开发出松散耦合(loose coupled)、可维护、可测试的代码和程序。这条原则的做法是大家熟知的面向接口,或者说是面向抽象编程。
如何重构代码达到DI的目的呢,下面是一个例子。
原代码:
class A{
public:
int Funtion1(B& obj) {
//do something
std::string str = “mock non-virtual methods using templates”;
auto rst = obj.Function2(str);
//do something
}
}
class B{
public:
int Funtion2(std::string _str){ puts(_str.c_str()); }
}
当我们对类A的方法Function1进行UT防护的时候,不关心其中类B的方法Function2的执行结果,这时候该如何对其进行mock呢(Function2是非虚的)?
在以上这种代码结构中,答案是无法进行mock!除非把Function2修改为virtual或者使用下面的方法:
修改后:
emplate <class T1 >
class RefactorA{
public:
int Funtion1(T1 & obj) {
//do something
std::string str = “mock non-virtual methods using templates”;
auto rst = obj.Function2(str);
//do something
}
}
重构之后,类RefactorA变成了类模板,在实例化的时候把依赖的类B显式的“注入”进去,这时候进行UT的时候,就可以把“注入”的类B的方法Function2 进行mock,代码如下:
//对类B中的Function2进行mock
class mockB
{
public:
MOCK_METHOD1(Funtion2, int (std::string ));
};
/对类A进行UT测试
class RefactorA _UT : public :: testing::Test
{
protected:
virtual void SetUp(){}
virtual void TearDown(){} RefactorA < mockB > mockObjA;//实例化模板类
}; TEST_F(RefactorA _UT , Funtion1)
{
//期望类B的方法Function2被调用至少一次,返回值为100,参数为任意字符串
mockB mockObjB;
EXPECT_CALL(mockObjB, Funtion2 (_))
.Times(AtLeast())
.WillOnce(Return()); auto rst = mockObjA.Function1( mockObjB );//注意这里传入的是mock出来的对象 EXPECT_TRUE( rst );
}
把类B的方法Function2 mock之后,UT的重点就可以放在对Function1的其它分支上了。
重点:将类A改写为类模板之后,在生产代码中,需要使用真正的类B对象来进行模板类的实例化,而在测试代码中,则需要使用mock出来的类B对象进行模板类的实例化。它们之间的无关的,这与mock接口类的虚函数有着本质的区别。
附:
类模板方法的声明和定义有以下4种方法,可以酌情使用:
① 荐做法是方法在定义的时候就进行实现(类RefactorA);
② 再者是,声明在模板类中,实现在模板类外,但要在一个文件中;
③ 把方法的实现写入xxx.inl文件,然后在模板类的结尾使用#include “xxx.inl”;
④ 把方法的实现写入xxx.cpp文件,但在cpp文件的最开始需要将模板类实例化。
总之,为了写出可UT的代码,需要时刻牢记“依赖注入”这个原则。
欢迎讨论。
mock non-virtual methods的更多相关文章
- What’s wrong with virtual methods called through an interface
May 31, 2016 Calling a virtual method through an interface always was a lot slower than calling a st ...
- why do we need virtual methods in C++?
http://stackoverflow.com/questions/2391679/why-do-we-need-virtual-methods-in-c Basic idea: when mark ...
- 【转载】#349 - The Difference Between Virtual and Non-Virtual Methods
In C#, virtual methods support polymorphism, by using a combination of the virtual and override keyw ...
- Rhino Mock
mock interfaces, delegates and classes, including those with parameterized constructors. set expecta ...
- [转载] google mock cookbook
原文: https://code.google.com/p/googlemock/wiki/CookBook Creating Mock Classes Mocking Private or Prot ...
- CLR via C# 3rd - 08 - Methods
Kinds of methods Constructors Type constructors Overload operators Type con ...
- 8.Methods(一)
1.Instance Constructors and Classes (Reference Types) Constructors methods : 1.allow an instance of ...
- (转) Virtual function
原文地址:http://en.wikipedia.org/wiki/Virtual_function In object-oriented programming, a virtual functio ...
- Should I expose asynchronous wrappers for synchronous methods?
Lately I've received several questions along the lines of the following, which I typically summarize ...
- why pure virtual function has definition 为什么可以在基类中实现纯虚函数
看了会音频,无意搜到一个frameworks/base/include/utils/Flattenable.h : virtual ~Flattenable() = 0; 所以查了下“纯虚函数定义实现 ...
随机推荐
- js 关于时间
var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-???? ...
- 00--C++牛人的博客
那些C++牛人的博客 这篇文章来自转载,转载的网址找不到了, 如果有人知道,可以在下面评论, 非常感谢,更感谢原作者. 现整理收集C++世界里那些“牛人”的个人博客.凡三类:一是令人高山仰止的 ...
- RxSwift文档搜集与备份
http://reactivex.io The Observer pattern done right ReactiveX is a combination of the best ideas fro ...
- Pyhhon中一些常见的字符串操作.
可变变量:list, 字典 不可变变量:元祖,字符串 字符串的操作(去掉空格, 切片, 查找, 连接, 分割, 转换首字母大写, 转换字母大小写, 判断是否是数字字母, 成员运算符(in / not ...
- Centos 7 安装图形化界面
Centos 7 默认是没有图形化界面的,但我们很多人在习惯了 Windows 的图形化界面之后,总是希望有一个图形化界面从而方便我们使用,这里介绍一下 CentOS7安装图形化桌面系统的方法. 一. ...
- jQuery样式操作
获取样式和设置样式 <p class='myClass' title='this is p'>this is p</p> 样式其实就是class属性所以设置和获取样式都能用a ...
- centos7把编译安装的服务通过systemctl管理
nginx编译安装的目录是/usr/local/nginx nginx配置文件是/usr/local/nginx/conf/nginx.conf systemctl管理的服务文件在/usr/lib/s ...
- 2016年工作中遇到的问题1-10:select-for-update锁表
1.select... for update锁表.注意事项:事务下使用,@Transactional如果用主键,只锁住1行记录如果不用主键,会锁住多条记录,mysql下测试,查询1条,锁住1行,查询2 ...
- position:fixed div如何居中
div{position:fixed;margin:auto;left:0; right:0; top:0; bottom:0;width:200px; height:150px;}
- ioremap映射函数
一.ioremap() 函数 Linux在io.h头文件中声明了函数ioremap(),用来将I/O内存资源的物理地址映射到核心虚地址空间(3GB-4GB)中(这里是内核空间),原型如下: 1.ior ...