我目前正在为抽象类创建一个单元测试,名为Component
。 VS2008编译我的程序没有问题,所以我能够在解决方案中创建一个单元测试项目。有一件事我注意到,虽然是测试文件被创建时,有这些方法,我以前从未见过:如何正确测试抽象类
internal virtual Component CreateComponent()
{
// TODO: Instantiate an appropriate concrete class.
Component target = null;
return target;
}
internal virtual Component_Accessor CreateComponent_Accessor()
{
// TODO: Instantiate an appropriate concrete class.
Component_Accessor target = null;
return target;
}
我相信这是创建一个具体Component
类。
在每个测试方法,有这样一行:
Component target = CreateComponent(); // TODO: Initialize to an appropriate value
我怎么初始化这个适当的值?或者,我如何实例化一个合适的具体类,如上面的CreateComponent
和CreateComponent_Accessor
方法所述?
这里是抽象类的构造函数,额外的信息:
protected Component(eVtCompId inComponentId, eLayer inLayerId, IF_SystemMessageHandler inMessageHandler)
只是为了澄清,与_mock实现你的意思是这个抽象class_的,创建一个派生类,并实现所有抽象成员... – DHN 2013-03-04 08:25:41
这将是非常有益的,如果你会告诉我这两个模拟实现的一些例子并使用一个模拟框架(我有Moq框架,一直在使用它的接口) – Anthony 2013-03-04 08:25:51
当然,我已经更新了我的答案,为您提供一个示例。 – 2013-03-04 08:39:24