I read the book Effectively Working with Legacy Code, and I played with the concept of overriding complex testing methods in unit tests by creating fakes. I put together an example of what I thought would work, and this led to behavior different from what I expected. I think I just opened a hole in my understanding of how method inheritance and overloading work in C #, and I was wondering if anyone could help me understand what is going on here.
I have the following interface:
public interface IAnimal { void MakeSound(); void Move(); }
Then I create an implementation of the animal interface as follows:
public class Dog : IAnimal { public void MakeSound() { Console.WriteLine("Woof"); } public void Move() { Console.WriteLine("Moved"); } }
When I use this class as follows:
IAnimal myanimal = new Dog(); myanimal.MakeSound(); myanimal.Move();
I get the following conclusion: Woof Moved
Now let's pretend that I need the unit test class Dog, but one of the methods, MakeSound (), must be overridden, because for some reason it makes testing the class difficult.
I create a fake dog by extending the Dog class and creating the MakeSound method
public class FakeDog : Dog { public void MakeSound() { Console.WriteLine("Bark"); } }
When I use this class as follows:
IAnimal myanimal = new FakeDog(); myanimal.MakeSound(); myanimal.Move();
I get the following conclusion: Woof Moved
I expected it to be: Lai Moved
However, if I have a FakeDog class, implement the animal interface and use it:
public class FakeDog : Dog, IAnimal { public void MakeSound() { Console.WriteLine("Bark"); } }
I get the following output: Lai Moved
I just want to understand why this now cancels the method, as I expected when the Dog class was just extended. Can anyone direct me directly to this?