C# Unit Testing – Should Virtual Methods Be Used to Make Mocking Possible?

cmockingunit testing

So far I have used to write interfaces to those classes I want to mock/fake. Reason for that is that those classes don't have any virtual method to overwrite. But resently I have figured out that I could use virtual methods also.

When should I use virtual methods rather than interfaces?

Should I even make most of methods virtual to make faking easier in the future?

Best Answer

How would virtual methods help? The idea of mocking is that you rip out a class completely from your application and plug in a completely different mocked class, with the only thing in common that they both implement the same interface. Inheritance doesn't come into the game at all.