Mocks and Spies

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 mocks and spies.

Mocks and spies make it possible to test code in isolation by taking the place of real dependencies. They were developed as a tool for testing how objects interact. Unfortunately, in common practice, mock-based tests have several problems. They tend to be hard to understand and make code harder to refactor.

Our challenge this week: test-drive a simple command-line application using mocks, then fix the common problems mock-based tests encounter. We need to test our code's interactions without making tests that are difficult to understand or modify.

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

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.