Member-only story
Mocking Too Much? Time to Question Your Design
And how to leverage mocks removal to improve your code design.
I could state it as a law: “Design quality is inversely proportional to mock usage”, and I probably wouldn’t be too far from reality. We all know that we should avoid mocks as much as possible because mocks make tests fragile. Yet, it turns out that mocks are also pointing to weak spots in our designs. Spots where the coupling is too high. So, we can leverage this insight and apply some patterns to improve our design while decreasing the number of required mocks.
The mocks that we know
Mocks are an essential part of testing. They allow us to test our software where it interfaces with other components.
Are you accessing a database? You need to mock it to ensure you have the correct data. Do you have a service? You need to mock it to ensure it behaves predictably. If the interaction with the screen is very slow to execute, you also need to mock it to achieve more speed. And so, little by little, the mocks keep adding up.
So, mocks are necessary.
Yet, we all know that mocks make testing more fragile. Each mock we add is one less verification. A weak spot. If the service changes its API, the mock won’t let us know that the…
