Using Test Doubles to Remove Dependencies

Learn the advantages of test doubles with the help of a test, and the problems we might face when removing dependencies.

If we don’t want to start at 20,000 feet with acceptance tests, test doubles are another way to get tests started without disrupting the untested code.

Test double advantages

In a legacy context, the advantage to using test doubles is their ability to isolate a single class and method from the rest of the application. When working with a legacy application, this allows us to temporarily put aside the issue of how shaky the rest of the application may be and focus on the single part we’re trying to figure out at that very moment.

In practice, this is similar to the test-double behavior-centric tests we saw in the chapter “Using Test Doubles as Mocks and Stubs,” except that the code already exists, so we don’t have the element of designing the API.

Example

Let’s look at an example. Take the following legacy method (which we’ll assume is part of some nebulous order model):

Get hands-on with 1200+ tech skills courses.