Search code examples
unit-testingnsubstitutetightly-coupled-code

Nsubstitute intercept hard dependency


I am unit testing legacy code and I am dealing with a class that instantiates another class. I believe this is testable using Microsoft Fakes, but am wondering if NSubstitute has the capability. I believe the answer is no, but need to be sure.

    public class ClassA
    {
        public int MethodA()
        {
            int reportId = this.MethodB();
            return reportId;
        }
        public virtual int MethodB()
        {
            ClassC c = new ClassC();
            return c.MethodA();
        }
    }

   public class ClassC
   {
       public virtual int MethodA()
       {
        return 2;
       }
   }
    [Test]
    public void Test_ClassA()
    {
        ClassA subclassA = new ClassA();
        var subclassC = Substitute.For<ClassC>();  //this is pointless the way I have it here
        subclassC.MethodA().Returns(1);            //this is pointless the way I have it here
        int outValue = subclassA.MethodA();
        Assert.AreEqual(outValue, 1);  //outvalue is 2 but I would like it to be 1 if possible using Nsubstitute
    }

Solution

  • It is possible to override virtual methods in classes by using partial substitution: just make sure that the base code cannot be called by specifying that the base class must not be called:

    var A = Substitute.ForPartsOf<ClassA>();
    var C = Substitute.ForPartsOf<ClassC>();
    
    C.When(c => c.MethodA()).DoNotCallBase();
    C.MethodA().Returns(10);
    A.When(a => a.MethodB()).DoNotCallBase();
    var cResult = C.MethodA();
    A.MethodB().Returns(cResult);
    
    Console.WriteLine(A.MethodB());
    Console.WriteLine(C.MethodA());