预计在GMock打电话,工厂返回unique_ptr

Expect call in GMock with Factory returning unique_ptr

本文关键字:返回 unique ptr 工厂 GMock 打电话      更新时间:2023-10-16

我有工厂通过返回unique_ptr来创建一些窗口:

std::unique_ptr<WindowsInterface> NCursesWindowsFactory::createMainWindow()
{
return std::make_unique<NCursesMainWindowDecorator>(std::make_unique<NCursesWindow>());
}

在模拟工厂类中,此方法:

MOCK_METHOD0(createMainWindow, std::unique_ptr<WindowsInterface>());

如何编写EXPECT_CALL,在不复制的情况下将某些对象作为unique_ptr返回,就像我在拳头方法中所做的那样?
我在工厂模拟上的EXPECT_CALL是这样返回的:

.WillOnce(Return(std::make_unique<NCursesMainWindowDecorator>(std::make_unique<NCursesWindow>())))

我想移动这个unique_ptr但 gmock 试图复制它:

./lib/googletest/googlemock/include/gmock/gmock-actions.h:579:59: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = miniReader::windowsManager::WindowsInterface; _Dp = std::default_delete<miniReader::windowsManager::WindowsInterface>]’

也许有点晚了,但这里有一个选项:

只需添加"ByMove"附加操作即可移动参数:

.WillOnce(Return(ByMove(std::make_unique<NCursesMainWindowDecorator>(std::make_unique<NCursesWindow>()))))