Testing Without Mocks

Each week in my Tuesday Lunch & Learn livestream, we look at a useful technical skill, define a challenge related to that skill, and solve the challenge live. This week, it’s testing without mocks.

Although mocks (and spies) are useful for testing interactions and isolating code, that isolation comes at a cost. Mocks can only check if dependencies’ methods are being called. They can’t check if they’re being called correctly. As a result, mock-based tests must be supplemented with integration tests, and these tests are slow and expensive. Wouldn’t it be nice if we could write unit tests that didn’t need additional integration testing?

Our challenge this week: given a simple command-line application, update the existing mock-based tests so they work without mocks, spies, or integration tests. The end result is surprisingly effective: tests that are more reliable than mock-based tests, faster, and easier to read.

To follow along, download the code from GitHub and check out the 2020-05-26 tag. To see the final result, check out the 2020-05-26-end tag or view it on GitHub.

For more details about testing without mocks, see the Testing Without Mocks Pattern Language.

Visit the Lunch & Learn archive for more.

If you liked this entry, check out my best writing and presentations, and consider subscribing to updates by email or RSS.