mardi 7 juillet 2015

What's the use of testing the interface rather than imeplementation?

I'm new to nSubstitute. I saw code samples like this in many articles. What's use of testing the interface? When will the following test fail? Will the following test fail based on the actual implementation of IMathService? The idea here is to write this first in TDD and then remove the substitute and put the real implementation? Or the idea is to test some real implementation but substitute the dependencies with Nsubstitute? If yes, this example doesn't show that. I'm confused.

 public interface IMathService
    {
        int Add(int a, int b);
    }

   [Test]
   public void nSubstituteMockingInterface()
   {
       var mathService = Substitute.For<IMathService>();
       mathService.Add(2,3).Returns(4);
       Assert.IsTrue(mathService.Add(2, 3) == 4);
   }

Let's say I have the following implementation, will the above test fail? The line Substitute.For() will return the instance of MyMathService? I believe, it'S not. If yes, what happens if I have more than one implementation for IMathService

   class MyMathService:  IMathService
    {
        public int Add(int a, int b)
        {
            throw new Exception("error");
        }
    }

Aucun commentaire:

Enregistrer un commentaire