C++ Google 可以模拟具有智能指针返回类型的方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7616475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can Google Mock a method with a smart pointer return type?
提问by Matthew Reddington
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.
我有一个返回智能指针的工厂。无论我使用什么智能指针,我都无法让 Google 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>());
And I get:
我得到:
"...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.
定义了智能指针中指向的类型。
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.
我知道它试图访问声明为私有的构造函数之一,但我不明白为什么。当这是一个 std::auto_ptr 时,错误说没有复制构造函数,这让我很困惑。
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...)?
无论如何,有没有办法模拟返回智能指针的方法?或者有没有更好的建厂方式?我唯一的决心是返回一个原始指针(blech ...)吗?
My environment is Visual Studio 2010 Ultimate and Windows 7. I'm not using CLI.
我的环境是 Visual Studio 2010 Ultimate 和 Windows 7。我没有使用 CLI。
采纳答案by VladLosev
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 要求模拟方法的参数和返回值是可复制的。根据boost 的文档,unique_ptr 是不可复制的。您可以选择返回使用共享所有权(shared_ptr、linked_ptr 等)的智能指针类之一,因此是可复制的。或者您可以使用原始指针。由于所讨论的方法显然是构造对象的方法,因此我认为返回原始指针没有固有的问题。只要您将结果分配给每个调用站点的某个共享指针,您就会没事。
回答by π?ντα ?ε?
A feasible workaround for google mock framework's problems with non (const) copyable function arguments and retun values is to use proxy mock methods.
对于 google mock 框架的非(const)可复制函数参数和返回值问题,一个可行的解决方法是使用代理模拟方法。
Suppose you have the following interface definition (if it's good style to use std::unique_ptr
in this way seems to be more or less a philosophical question, I personally like it to enforce transfer of ownership):
假设您有以下接口定义(如果std::unique_ptr
以这种方式使用的好风格似乎或多或少是一个哲学问题,我个人喜欢它来强制转让所有权):
class IFooInterface {
public:
virtual void nonCopyableParam(std::unique_ptr<IMyObjectThing> uPtr) = 0;
virtual std::unique_ptr<IMyObjectThing> nonCopyableReturn() = 0;
virtual ~IFooInterface() {}
};
The appropriate mock class could be defined like this:
适当的模拟类可以这样定义:
class FooInterfaceMock
: public IFooInterface {
public:
FooInterfaceMock() {}
virtual ~FooInterfaceMock() {}
virtual void nonCopyableParam(std::unique_ptr<IMyObjectThing> uPtr) {
nonCopyableParamProxy(uPtr.get());
}
virtual std::unique_ptr<IMyObjectThing> nonCopyableReturn() {
return std::unique_ptr<IMyObjectThing>(nonCopyableReturnProxy());
}
MOCK_METHOD1(nonCopyableParamProxy,void (IMyObjectThing*));
MOCK_METHOD0(nonCopyableReturnProxy,IMyObjectThing* ());
};
You just need to take care, that configurations (Actions taken) for the nonCopyableReturnProxy()
method return either NULL
or an instance allocated dynamically on the heap.
您只需要注意,该方法的配置(采取的操作)nonCopyableReturnProxy()
要么返回,要么返回NULL
在堆上动态分配的实例。
There's a google-mock user forum threaddiscussing this topic where one of the maintainers states that the google-mock framework won't be changed to support this in future arguing that their policies strongly discourage the usage std::auto_ptr
parameters. As mentioned this is IMHO a philosophical point of view, and the capabilities of the mocking framework shouldn't steer what kind of interfaces you want to design or you can use from 3rd party APIs.
有一个google-mock 用户论坛线程讨论了这个主题,其中一位维护者表示 google-mock 框架在未来不会更改以支持这一点,并认为他们的政策强烈反对使用std::auto_ptr
参数。如前所述,恕我直言,这是一个哲学观点,模拟框架的功能不应该控制您想要设计的接口类型,或者您可以从 3rd 方 API 使用。
As said the answer describes a feasibleworkaround.
如上所述,答案描述了一种可行的解决方法。
回答by jeff
I know this post was from a long time ago, so you've probably discovered the answer by now.
我知道这篇文章是很久以前的,所以你现在可能已经找到了答案。
gmock previously did not support mock functions that returned any movable type, including smart pointers. However, in April 2017, gmock introduced a new Action modifier ByMove
.
gmock 以前不支持返回任何可移动类型的模拟函数,包括智能指针。然而,在 2017 年 4 月,gmock 引入了一个新的 Action 修饰符ByMove
。
EXPECT_CALL(*foo_, Bar(_, )).WillOnce(Return(ByMove(some_move_only_object)));
EXPECT_CALL(*foo_, Bar(_, )).WillOnce(Return(ByMove(some_move_only_object)));
where some_move_only_object can be e.g. a std::unique_ptr.
其中 some_move_only_object 可以是例如 std::unique_ptr.
So yes, now gmock can mock a function that takes a smart pointer.
所以是的,现在 gmock 可以模拟一个带有智能指针的函数。
回答by Andrey Borisovich
I have recently discovered that returning smart pointers by mocked functions is still not very user friendly. Yes, the new action ByMove
had been introduced, however it turns out it may be used only once during the test. So imagine you have a factory class to test that repeatedly returns unique_ptr
to the newly created object.
我最近发现通过模拟函数返回智能指针仍然不是很用户友好。是的,ByMove
已经引入了新动作,但结果证明它在测试期间只能使用一次。所以想象你有一个工厂类来测试它反复返回unique_ptr
到新创建的对象。
Any attempt to .WillRepeatedly(Return(ByMove)
or multiple ON_CALL
with .WillByDefault(Return(ByMove)
will result in the following error:
任何尝试.WillRepeatedly(Return(ByMove)
或多次ON_CALL
使用.WillByDefault(Return(ByMove)
都会导致以下错误:
[ FATAL ] Condition !performed_ failed. A ByMove() action should only be performed once.
[致命] 条件 !performed_ 失败。ByMove() 操作应该只执行一次。
This is also clearly stated in the GMock Cookbookin the paragraph "Mocking Methods That Use Move-Only Types".
这在GMock Cookbook的“Mocking Methods That Use Move-Only Types”段落中也有明确说明。
回答by dstm
in the mock class put same as you want
在模拟课上放你想要的一样
MOCK_METHOD0(Create, std::unique_ptr());
MOCK_METHOD0(创建,std::unique_ptr());
and in the test as below
并在测试如下
EXPECT_CALL(<mock-obj>, Create())
.WillOnce([]()->std::unique_ptr<IMyObjectThing>{
return std::make_unique<IMyObjectThing>();
});
if vs 2010 not support for lambda, you can use a functor
如果 vs 2010 不支持 lambda,则可以使用函子