Working with mocks is a lot like working out: if you don’t know what you’re doing then you’ll either a) don’t get the results you want or b) hurt yourself. And in the worst case, you’ll get both and be unhappy with the results and swear off mocking as a useless practice.
This talk will show some common pitfalls of using mocks (updating a lot of mocks to return the new correct value, mocking out the same complex thing again and again, elaborate setups to exercise a piece of business logic, and more) and how you can constrain how you work with mocks to make them work for you and make your test suite something you’ll love to maintain.
Presented at GopherCon Singapore.
If you find this talk interesting check out How to design a test suite you'll love to maintain which talks about how I design test suites in general and it has a recording.
Resources (links from the last slide):
- Please Don't Mock Me by Justin Searls. Justin gives much more detailed explanation of why it's worth being this strict with how you use mocks. He is a fantastic presenter on testing and his back catalog is worth watching
- Growing Object-Oriented Software Guided by Tests by Steve Freeman and Nat Pryce
This book teaches you how to grow your applications test-first and outside-in using mocks to help you.
- Example of a builder to configure complex mocks. Because working with more complex cases can get quite wordy and this is a pattern to name the setup and make it reusable.
- How to design a test suite you'll love to maintain by Björn Andersson (me!). My sister talk about how I work with tests overall to make them maintainable, there's a link to a recording there
- The Practical Test Pyramid by Ham Vocke, from Martin Fowler's website. A detailed look at different type of tests and how they work together
- All the little things by Sandi Metz. A refactoring of the Gilded Rose kata into many small objects and why that is great