[英]Do derived classes still depend on the imports of the parent class, even if the method referencing another assembly is being overridden?
我有一個引用外部程序集 (AutoCAD) 的父 class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.EditorInput;
namespace WBPlugin
{
public class DoubleInputRetriever : IUserInputRetriever<Double>
{
public double getUserInput(string prompt)
{
return getUserInput(prompt, 16);
}
public virtual double getUserInput(String prompt, Double defaultValue)
{
Editor ed = Active.Editor;
PromptDoubleOptions pdo = new PromptDoubleOptions(prompt);
pdo.DefaultValue = defaultValue;
pdo.AllowNone = true;
pdo.AllowNegative = false;
PromptDoubleResult pdr = ed.GetDouble(pdo);
if (pdr.Status != PromptStatus.OK)
{
ed.WriteMessage("\n*Cancel*");
return 0;
}
if (pdr.Status == PromptStatus.None)
{
return defaultValue;
}
return pdr.Value;
}
}
}
然后是一個子 class,我試圖將“假”數據輸入我的工具,以便能夠在 AutoCAD 之外對其進行單元測試。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WBPlugin;
namespace WBPluginTests.Fakes
{
public class FakeDoubleInputRetriever : DoubleInputRetriever
{
public double ReturnValue { get; set; }
public FakeDoubleInputRetriever()
{
}
public FakeDoubleInputRetriever(Double value)
{
ReturnValue = value;
}
public override double getUserInput(string prompt, double defaultValue)
{
return ReturnValue;
}
}
}
我無法運行單元測試,因為它找不到某個 AutoCAD 程序集,這是有道理的,因為我試圖在 AutoCAD 之外進行測試,因此該程序集不可用。
問題是父 class 正在嘗試運行但由於找不到所需的程序集而無法運行嗎? 即使它是我在測試中使用的子 class,以及將使用的覆蓋方法?
繼承是最強的依賴。 創建一個假的實現IUserInputRetriever<Double>
將您與依賴於 AutoCAD 的具體實現分離。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.