简体   繁体   中英

Expect call in GMock with Factory returning unique_ptr

I have factory that creates some windows by returning unique_ptr:

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

And in Mocked factory class this method:

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

How to write EXPECT_CALL that will return some object as unique_ptr without copying it, just as I do in my fist method?
My EXPECT_CALL on factory mock has return like this:

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

And I want to move this unique_ptr but gmock tries to copy that:

./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>]’

Maybe a bit late, but here an option:

Just add 'ByMove' additional action to move the argument:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM