如何对工厂进行单元测试?
我通过为所有类提供一个接口来对我的类进行单元测试。这些接口又具有自己的模拟。
但假设我有以下内容:
class IData
{
GetData()
}
class IOnScreenDataCalculator
{
Calculate(IData)
}
class OnScreenData : IOnScreenData
{
OnScreenData(PTR_T(IData), PTR_T(IOnScreenDataCalculator))
enter code here
GetOnScreenData()
}
现在假设我希望拥有多个用于不同类型的数据和计算器的工厂。我如何对这些工厂进行单元测试,我的工厂如下:
OnScreenBlueDataForWideScreenFactory
{
PTR:T(IOnScreenData) Create()
{
PTR_T(Data) data = ptr_t(new BlueData());
PTR_T(IOnScreenDataCalculator) calculator = ptr_t(new WideScreenDataCalculator());
PTR_T(IOnScreenData) onScreenData = ptr_t(new WideScreenDataCalculator(data, calculator ));
return onScreenData;
}
}
谢谢你的帮助,
巴里。
I unit test my classes by giving all my classes an interface. These interfaces have in turn their own mocks.
But lets say I have the following:
class IData
{
GetData()
}
class IOnScreenDataCalculator
{
Calculate(IData)
}
class OnScreenData : IOnScreenData
{
OnScreenData(PTR_T(IData), PTR_T(IOnScreenDataCalculator))
enter code here
GetOnScreenData()
}
Now lets say that I wish to have a number of factories for different types of data and calculators. How can I unit test these factories where my factories are as follows:
OnScreenBlueDataForWideScreenFactory
{
PTR:T(IOnScreenData) Create()
{
PTR_T(Data) data = ptr_t(new BlueData());
PTR_T(IOnScreenDataCalculator) calculator = ptr_t(new WideScreenDataCalculator());
PTR_T(IOnScreenData) onScreenData = ptr_t(new WideScreenDataCalculator(data, calculator ));
return onScreenData;
}
}
Thanks for your help,
Barry.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定代码片段是否真的是 C++,但示例应该是这样的:
以及单元测试:
I am not sure the code snippets are really c++, but the example should be something like this :
and the unit test:
我将调用 Create() 并验证我是否获得了具有正确构成类型的正确构造的对象。
I'd call Create() and verify that I get a properly constructed object with the right constituent types.