我现在正在尝试用gmock / gtest对工厂进行单元测试,但是我无法让我的测试实际使用我想测试的模拟对象,这时我觉得自己做一些根本错误的事情。
我具有以下架构(不包括标题) 与工厂和对象工厂:
class IObject
{
public:
virtual ~IObject() {};
virtual void objectFunction(int someValue) = 0;
};
using ObjectPtr = std::unique_ptr<IObject>;
class IObjectFactory
{
public:
virtual ~IObjectFactory() {};
virtual std::unique_ptr<IObject> create() = 0;
};
using ObjectFactoryPtr = std::unique_ptr<IObjectFactory>;
ObjectFactory类返回对象类的实例,如下所示:
ObjectFactory.h
class ObjectFactory : public IObjectFactory
{
public:
ObjectFactory() {};
~ObjectFactory() override {};
std::unique_ptr<IObject> create() override
{
return std::make_unique<Object>();
}
};
我也有Collection Class
ICollection.h
class ICollection
{
public:
virtual ~ICollection() {};
virtual void someFunction(int value) = 0;
};
Collection.h
class Collection : public ICollection
{
public:
Collection(IParameter *parameter, double& slider, FilterFactoryPtr&& filterFactory);
~Collection() override;
private:
ObjectFactoryPtr objectFactory_ {};
ObjectPtr object_ {};
Collection类将ObjectFactory注入其构造函数中,并在构造函数中使用它创建Object的实例,如下所示:
Collection.cpp
Collection::Collection(IParameter *parameter, double aValue, ObjectFactoryPtr&& objectFactory)
: objectFactory (std::move(objectFactory))
{
object_ = objectFactory->create();
}
最后,在Collection类的函数调用中,将调用Object Class中的objectFunction。
为了测试Object,ObjectFactory和Collection的行为,我编写了一些类似的模拟:
ObjectMock.h
class ObjectMock : public IMock
{
public:
virtual ~ObjectMock() {}
MOCK_METHOD1(objectFunction, void(int someValue));
};
ObjectFactoryMock.h
class ObjectFactoryMock : public IObjectFactory
{
public:
virtual ~ObjectFactoryMock() {}
virtual std::unique_ptr<IObject> create()
{
return std::unique_ptr<dearvrDir::IObject>(createProxy());
}
MOCK_METHOD0(createProxy, IObject* ());
}
ParameterMock.h
class ParameterMock : public IParameterMock
{
public:
virtual ~ParameterMock() {}
MOCK_CONST_METHOD0(getValue, double());
}
最后,我想运行以下测试来验证objectFactory对象的create()调用:
class UnitTest_CollectionTestCase : public ::testing::Test
{
protected:
std::unique_ptr<Collection> collection_;
ParameterMock parameterMock_;
};
TEST_F(UnitTest_CollectionTestCase, calls_create_on_factory)
{
double value = 123;
collection_ = std::make_unique<Collection>(¶meterMock_, value, std::make_unique<ObjectFactoryMock>());
auto&& objectFactoryMock = std::make_unique<NiceMock<ObjectFactoryMock>>();
ON_CALL(*objectFactoryMock, create())
.WillByDefault(Return(std::make_unique<Object>));
}
但是,我得到了以下错误,而不是测试结果,暗示了我的退货预期:
错误:没有匹配的函数来调用“ ImplicitCast_” value_(ImplicitCast_(value_before_cast_)){} ^ ~~~~~~~~~~~~~~~~~~~~ 注意:在实例化成员函数'testing :: internal :: ReturnAction>(*)()> :: Impl,std :: __ 1 :: default_delete>(*)(),IObject *()> :: Impl'被请求这里 返回Action(new Impl(value_)); ^ 注意:在实例化功能模板时,此处要求'testing :: internal :: ReturnAction>(*)()> :: operator Action' .WillByDefault(Return(std :: make_unique));
在这一点上,我有点绝望,很高兴听到有关该主题的任何建议。
先谢谢了, 西蒙
答案 0 :(得分:1)
原来,我只需要在ON_CALL语句中添加“ ByMove”,以指示我的模拟对象不要调用复制构造函数,该构造函数由于unique_ptr而被删除。
因此,声明
ON_CALL(*objectFactoryMock, create())
.WillByDefault(Return(ByMove((std::make_unique<Object>())));
必须是
using System.IO;