Dependency injection is great.
Making your business logic dependent on interfaces is great.
But, did you ever find it cumbersome to mock those interfaces such that all of your business logic is well exercised?
Yeah, me too. In this article I’ll demonstrate how I organize my mocks to keep the bloat to a minimum.
As developers, we are often faced with legacy code bases that were never designed with unit testing in mind. Of course, we now know the value of unit tests and we want to test as much as possible. A good place to start implementing tests in old code is to write tests around each bug and feature. Do not try to refactor the entire pile of rubble, but instead begin with tests around just the code you are adding or changing.
Here I will show you a simple technique to refactor “just enough” to get some tests running without introducing unacceptable risk in your modifications. The examples with be in C#, but the technique applies everywhere.