使用Google Mock指向界面的指针

问题描述 投票:0回答:1

我有以下界面:

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
        ]
c++ c++17 googlemock
1个回答
1
投票

首先EXPECT_CALL获取参考,而不是(智能)指针。其次,它必须引用具体的模拟,而不是模拟的类/接口。第三,最新的gtest中没有Will功能。有WillOnceWillRepeadately。所以解决方法是这样的:

std::unique_ptr<BackgroundModelMock> model = std::make_unique<BackgroundModelMock>();
Image input;
Image output;
EXPECT_CALL(*model, process(input)).WillOnce(testing::Return(output));

© www.soinside.com 2019 - 2024. All rights reserved.