c++ - 如何将 unique_ptr 作为函数参数传递给模拟对象
问题描述
我正在尝试使用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库,所以我不添加这个类的实现。
解决方案
推荐阅读
- implementation - Charm 加密库中的 BN 曲线
- ios - Swift 在运行时获取`Scheme Name 或 Swift Compiler - Custome Flags` 值
- azure - 如何确保我的 Azure Web 应用程序“足够快”地为海外客户提供服务?
- protractor - 量角器没有在 .pause() 方法上停止
- spring-boot - Spring Boot 无法读取 yml 属性文件
- ibm-mobilefirst - IBM MFP 控制台适配器显示
- ios - 无法快速接收推送通知 5 IOS 13
- java - 由于 Lambda 函数,JIT 编译期间的本机内存峰值
- typescript - 打字稿可以根据条件表达式和常量推断类型吗?
- kubernetes-helm - Helm 未初始化