我目前正在爲抽象類創建一個單元測試,名爲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