我可以不模拟 C++ 接口中的所有方法吗?当使用googlemock时
我正在使用 Google Mock 1.6 RC,并尝试模拟 COM 接口
。 COM 接口中有近 50 个方法,其中一些方法是从基接口继承的。当我创建一个继承此接口的模拟struct
并仅模拟我正在使用的方法时,我收到无法实例化抽象类
错误。
我想知道是否可以在 googlemock 中执行此操作。
I am using Google Mock 1.6 RC and am trying to Mock a COM Interface
. There are close to 50 methods in the COM Interface some of which are inherited from base interfaces. 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.
I want to know if it is possible to do this in googlemock or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是不可能的。您必须重载所有接口中的所有纯虚方法(构造函数和析构函数除外)。
It is not possible to do. You have to overload all pure virtual methods from all interfaces (except for the constructor and destructor).
您必须直接或间接重写在您继承的类中声明为纯虚拟的每个方法。不想把它们全部覆盖有两个原因:
(1) 的修复方法是使用 Google Mock 的
scripts
目录中的gmock_gen.py
脚本。它检查类定义并将方法声明转换为 MOCK_METHOD 语句。如果您对(2)有疑问,您可以用存根替换不必要的 MOCK_METHOD 语句:抛出
异常会提醒您调用特定存根的情况,这意味着您可能需要模拟相反。
编辑:如果要模拟的原始接口是使用 Microsoft 的宏编写的,此线程< /a> 发布了一个脚本,将它们转换为
gmock_gen.py
可接受的 C++。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:
The fix for (1) is to use the
gmock_gen.py
script in Google Mock'sscripts
directory. It goes over the class definition and converts method declarations into theMOCK_METHOD
statements. If you have problems with (2), you can replace the unnecessaryMOCK_METHOD
statements with stubs:with
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
.我不完全确定是否所有方法都应该包含在模拟类中...在 gmock 示例中,您可以看到例如析构函数没有被模拟。因此我认为没有必要嘲笑整个班级。
不管怎样,你不应该创建模拟类而不是模拟结构吗?
但是,scripts/generator 中有一个 gmock_gen.py 工具,可以为您完成模拟大型类的艰苦工作。
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. 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.