我可以不模拟 C++ 接口中的所有方法吗?当使用googlemock时

发布于 2024-10-28 03:28:20 字数 202 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以可爱出名 2024-11-04 03:28:20

这是不可能的。您必须重载所有接口中的所有纯虚方法(构造函数和析构函数除外)。

It is not possible to do. You have to overload all pure virtual methods from all interfaces (except for the constructor and destructor).

百变从容 2024-11-04 03:28:20

您必须直接或间接重写在您继承的类中声明为纯虚拟的每个方法。不想把它们全部覆盖有两个原因:

  1. 它们太多了,而且你有更好的事情可以做,而不是把它们全部覆盖。
  2. 编译所有模拟类的模拟类太慢并且占用太多内存。

(1) 的修复方法是使用 Google Mock 的 scripts 目录中的 gmock_gen.py 脚本。它检查类定义并将方法声明转换为 MOCK_METHOD 语句。如果您对(2)有疑问,您可以用存根替换不必要的 MOCK_METHOD 语句:

MOCK_METHOD1(f, bool(int i));

抛出

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

异常会提醒您调用特定存根的情况,这意味着您可能需要模拟相反。

编辑:如果要模拟的原始接口是使用 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:

  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. It goes over the class definition and converts method declarations into the MOCK_METHOD statements. If you have problems with (2), you can replace the unnecessary MOCK_METHOD statements with stubs:

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.

祁梦 2024-11-04 03:28:20

我不完全确定是否所有方法都应该包含在模拟类中...在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文