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, easier to read, and run 300x faster.
To follow along, download the code from https://github.com/jamesshore/livestream 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 at https://github.com/jamesshore/livestr....