如何将unique_ptr传递给模拟对象作为函数参数

时间:2019-10-19 11:23:51

标签: c++ googletest gmock

我正在尝试使用 gtest / gmock 将一些测试添加到我的项目中。我在测试将const unique_ptr带到另一个类的对象的函数时遇到问题。

View具有一些功能,包括get_description_from_user()get_category_from_user()。这些功能获取用户在终端窗口中输入的数据。 在类PlanService中,有一个函数create_plan()从类View中调用上述函数。我创建了一个MockView类,但是我不知道如何将指向该类对象的指针作为create_plan()函数的参数来传递。

这是我的代码:

class MockView :
        public View {
public:
    MOCK_METHOD(string, get_description_from_user, ( ));
    MOCK_METHOD(string, get_category_from_user, ( ));
};

TEST(create_plan, set_category_and_description)
{
    const unique_ptr<MockView> view(new MockView());
    EXPECT_CALL(*view, get_description_from_user()).WillOnce(Return("desc"));
    EXPECT_CALL(*view, get_category_from_user()).WillOnce(Return("cat"));

    PlanService plan_service;
    plan_service.create_plan(move(view)); //The problem is here.
    EXPECT_EQ(plan_service.get_category(), "cat");
    EXPECT_EQ(plan_service.get_description(), "desc");
}

我遇到错误:

no matching function for call to  ‘PlanService::create_plan(std::remove_reference<const  std::unique_ptr<MockView>&>::type)’ plan_service.create_plan(move(view));

非常感谢您的帮助。

编辑:PlanService

#include <memory>
#include <vector>

class Plan;
class View;

class PlanService {

public:
    PlanService() = default;
    virtual ~PlanService() = default;
    virtual void create_plan(const std::unique_ptr<View>& view);
    //... some more functions
    const std::string& get_description() const;
    const std::string& get_category() const;
private:
    std::string description;
    std::string category;
};

create_plan()函数的定义:

void PlanService::create_plan(const unique_ptr<View>& view)
{
    description = view->get_description_from_user();
    category = view->get_category_from_user();
}

View更为复杂,因为它使用ncurses库,因此我不添加此类的实现。

0 个答案:

没有答案
相关问题