vendredi 28 août 2015

Why are Interface Mocks More Desirable than Subclass Mocks when Unit Testing?

When a class implements an interface, then it is easy to mock, but, you have to create an interface for it.

You can also mock by subclassing and overriding. If your base class provides a parameterless protected constructor, then your subclass mocks are not tied to changes in the base class constructor.

At first glance, it seems like the subclassing method to creating mocks is more desirable than creating interfaces for everything, but obviously, that's not how most people are doing it.

So, why are mocks based on interfaces considered better practice than mocks based on subclassing?

Aucun commentaire:

Enregistrer un commentaire