簡體   English   中英

期望在 GMock 中調用 Factory 返回 unique_ptr

[英]Expect call in GMock with Factory returning unique_ptr

我有工廠通過返回 unique_ptr 創建一些 windows:

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

在 Mocked factory class 這個方法:

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

如何編寫 EXPECT_CALL 將返回一些 object 作為 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>()))))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM