可以Google Mock使用智能指针返回类型的方法吗? [英] Can Google Mock a method with a smart pointer return type?

查看:1152
本文介绍了可以Google Mock使用智能指针返回类型的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工厂,返回一个智能指针。无论我使用什么智能指针,我都无法获得Google Mock来模拟工厂方法。

I have a factory that returns a smart pointer. Regardless of what smart pointer I use, I can't get Google Mock to mock the factory method.

mock对象是纯抽象接口的实现,其中所有方法是虚拟的。我有一个原型:

The mock object is the implementation of a pure abstract interface where all methods are virtual. I have a prototype:

MOCK_METHOD0(Create, std::unique_ptr<IMyObjectThing>());

我得到:

"...gmock/gmock-spec-builders.h(1314): error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'"

定义智能指针指向的类型。

The type pointed to in the smart pointer is defined.

我得到它试图访问一个声明为私有的构造函数,但我不明白为什么。当这是一个std :: auto_ptr,错误说没有复制构造函数,这让我困惑。

And I get it's trying to access one of the constructors declared private, but I don't understand why. When this was an std::auto_ptr, the error said there was no copy constructor, which confuses me.

无论如何,有一种方法来模仿一个方法,智能指针?还是有更好的方式来建立一个工厂?是我唯一的决心返回一个原始指针(blech ...)?

Anyway, is there a way to Mock a method that returns a smart pointer? Or is there a better way to build a factory? Is my only resolve to return a raw pointer (blech...)?

我的环境是Visual Studio 2010 Ultimate和Windows 7.我没有使用CLI。 / p>

My environment is Visual Studio 2010 Ultimate and Windows 7. I'm not using CLI.

推荐答案

在大多数情况下,Google Mock需要可以复制模拟方法的参数和返回值。根据 boost的文档,unique_ptr不是可复制。您可以选择返回使用共享所有权的智能指针类之一( shared_ptr ,linked_ptr等),因此是可复制的。或者你可以使用原始指针。因为所讨论的方法显然是构造对象的方法,所以我没有看到返回原始指针的固有问题。只要在每个调用站点将结果分配给一些共享指针,你就会很好。

Google Mock requires parameters and return values of mocked methods to be copyable, in most cases. Per boost's documentation, unique_ptr is not copyable. You have an option of returning one of the smart pointer classes that use shared ownership (shared_ptr, linked_ptr, etc.) and are thus copyable. Or you can use a raw pointer. Since the method in question is apparently the method constructing an object, I see no inherent problem with returning a raw pointer. As long as you assign the result to some shared pointer at every call site, you are going to be fine.

这篇关于可以Google Mock使用智能指针返回类型的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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