Search code examples
c#unit-testingclassinheritanceautocad-plugin

Do derived classes still depend on the imports of the parent class, even if the method referencing another assembly is being overridden?


I have a parent class that references an outside assembly (AutoCAD)

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;
        }
    }
}

And then a child class which I made in an attempt to be able to feed in "fake" data to my tool to be able to unit test it outside of 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;
        }
    }
}

I am unable to run the unit tests because it can't find a certain AutoCAD assembly, which makes sense since I'm trying to do tests outside of AutoCAD so the assembly isn't available.

Is the problem that the parent class is trying to run but can't since the required assembly can't be found? Even though it's the child class that I'm using in the test, and the overridden method that would be used?


Solution

  • Inheritence is the strongest kind of dependency. Creating a fake implementing IUserInputRetriever<Double> decouples you from the concrete implementation that is depending on AutoCAD.