c++ - 使用带有接口指针的 Google Mock
问题描述
我有以下界面:
struct IBackgroundModel {
virtual Image process(const Image& inputImage) = 0;
virtual ~IBackgroundModel() = default;
};
和一个模拟测试:
TEST_F(EnvFixture, INCOMPLETE) {
struct BackgroundModelMock : IBackgroundModel {
MOCK_METHOD1(process, Image(const Image& override));
};
std::unique_ptr<IBackgroundModel> model = std::make_unique<BackgroundModelMock>();
Image input;
Image output;
EXPECT_CALL(model, process(input)).Will(Return(output));
BackgroundModelFactory factory;
factory.set(model.get());
const auto result = factory.process(input);
}
但我无法编译也无法弄清楚错误的含义:
error C2039: 'gmock_process': is not a member of 'std::unique_ptr<P,std::default_delete<P>>'
with
[
P=kv::backgroundmodel::IBackgroundModel
]
C:\Source\Kiwi\Kiwi.CoreBasedAnalysis\Libraries\Core\Kiwi.Vision.Core.Native\include\Ptr.hpp(17): message : see declaration of 'std::unique_ptr<P,std::default_delete<P>>'
with
[
P=kv::backgroundmodel::IBackgroundModel
]
解决方案
首先EXPECT_CALL
引用,而不是(智能)指针。其次,它必须引用具体的模拟,而不是模拟的类/接口。第三,在最新的 gtest 中没有功能Will
。有WillOnce
和WillRepeadately
。所以修复是这样的:
std::unique_ptr<BackgroundModelMock> model = std::make_unique<BackgroundModelMock>();
Image input;
Image output;
EXPECT_CALL(*model, process(input)).WillOnce(testing::Return(output));
推荐阅读
- vue.js - Vuex 访问数据属性
- java - 为什么我无法连接到 java 中的 MySQL 驱动程序数据库?
- java - 如何将设计库实现到 android 应用程序?
- c - C 中的指针是如何工作的?
- python - 覆盖 Python 元类 __getitem__
- html - 如何仅使用 css 和 html 重组并排并堆叠的 HTML 元素?
- reactjs - 在服务器渲染阶段围绕 Next.js 应用程序包装动态自定义皮肤
- ubuntu - FT232H 用户空间 GPIO 设备
- android - 如何使自定义视图在Android中继承其父样式
- pytorch - 未找到从源 libavcodec/avcodec.h 安装 torchvision