簡體   English   中英

即使引用另一個程序集的方法被覆蓋,派生類是否仍然依賴於父 class 的導入?

[英]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 的具體實現分離。

你需要做的就是模擬這個 class 用於你的單元測試。

https://github.com/moq/moq4

這是一個很棒的圖書館。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM