简体   繁体   English

使用googlemock时,我是否可以避免在C ++界面中模拟所有方法

[英]Can I get away with not mocking all methods in an interface in C++ when using googlemock

I am using Google Mock 1.6 RC and am trying to Mock a COM Interface . 我正在使用谷歌模拟1.6 RC,我正在尝试模拟COM Interface There are close to 50 methods in the COM Interface some of which are inherited from base interfaces. COM接口中有近50种方法,其中一些方法是从基接口继承的。 When I create a mock struct that inherits from this interface and mock only the methods I am using, I get the cannot instantiate abstract class error. 当我创建一个继承自此接口的模拟struct并仅模拟我正在使用的方法时,我得到了cannot instantiate abstract class错误。

I want to know if it is possible to do this in googlemock or not. 我想知道是否可以在googlemock中执行此操作。

It is not possible to do. 这是不可能的。 You have to overload all pure virtual methods from all interfaces (except for the constructor and destructor). 您必须从所有接口(构造函数和析构函数除外)重载所有纯虚方法。

You have to override every method that has been declared as pure virtual in the classes you inherit from, directly or indirectly. 您必须覆盖从您直接或间接继承的类中声明为纯虚拟的每个方法。 There are two reasons not to want override them all: 有两个原因不想要全部覆盖它们:

  1. There are too many of them and you have something better to do with your time than to go over them all. 它们太多了,你有更好的时间来处理你的所有事情。
  2. Compiling a mock class with all of them mocked out is too slow and takes too much memory. 编译一个模拟类,其中所有这些都被模拟出来太慢并且占用了太多内存。

The fix for (1) is to use the gmock_gen.py script in Google Mock's scripts directory. (1)的修复是使用Google Mock scripts目录中的gmock_gen.py脚本。 It goes over the class definition and converts method declarations into the MOCK_METHOD statements. 它遍历类定义并将方法声明转换为MOCK_METHOD语句。 If you have problems with (2), you can replace the unnecessary MOCK_METHOD statements with stubs: 如果您遇到(2)的问题,可以用存根替换不必要的MOCK_METHOD语句:

MOCK_METHOD1(f, bool(int i));

with

virtual bool f(int i) {
  thrown std::exception("The stub for f(int) has been invoked unexpectedly.");
}

Throwing an exception will alert you to a situation where a particular stub has been invoked, meaning you likely need to mock it instead. 抛出异常将提醒您已调用特定存根的情况,这意味着您可能需要模拟它。

Edit: If the original interfaces to mock are written using Microsoft's macros, this thread has a script posted that converts them to C++ acceptable to gmock_gen.py . 编辑:如果使用Microsoft的宏编写模拟的原始接口,则此线程会发布一个脚本,将它们转换为gmock_gen.py可接受的C ++。

I'm not entirely sure whether all methods should be covered in the mock class... In the gmock examples you can see that for example destructors are not mocked. 我不完全确定是否应该在mock类中涵盖所有方法...在gmock示例中,您可以看到例如析构函数不会被模拟。 Therefore I presume there is no need to mock the entire class. 因此我认为没有必要嘲笑整个班级。

Anyway, shouldn't you create mock class rather than mock struct? 无论如何,你不应该创建模拟类而不是模拟结构吗?

However, there is a gmock_gen.py tool in scripts/generator that should do the hard work of mocking large classes for you. 但是,脚本/生成器中有一个gmock_gen.py工具,可以为您完成模拟大类的艰苦工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM