3.1. The power of a test double

Mahatma Gandhi said, “Be the change you want to see in the world.” Test doubles fulfill Gandhi’s call to action by becoming the change you want to see in your code. Farfetched? Let me elaborate.

Code is a mass. It’s a network of code that refers to other code. Each of these chunks has a defined behavior—behavior you, the programmer, have defined for them. Some of that behavior is atomic and contained within a single class or method. Some of that behavior implies interaction between different chunks of code.

Every now and then, in order to verify that the behavior of one chunk of code is what you want it to be, your best option is to replace the code around it so that you gain full control of the environment in ...

Get Effective Unit Testing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.