Jason Gorman’s Mock Abuse

Dave Farley calls it the Mockery. Jason Gorman has a video calling it Mock Abuse and Mock Hell. Both are true. I’m dealing with it at work and trying to gather various resources to help teach why this is a bad thing. I think Jason’s video has the best summary I’ve ever seen on it because “too many mocks” really are a symptom of a bad design.

He cites and visualizes all the problems this can cause like inability to refactor, creates a loathing for your test suite, and the cost of focusing on high coverage instead of validating behavior. He also shows what you should do instead, like create sections of the code that focus one thing, expose a public API, and you don’t care internally if they have 1 or 50 classes, or 1 or 50 functions. This makes it much easier to simplify the design, test these collaborators together, and most importantly “easily change the code”.

The focus of the video is OOP, and to true to form, OOP code does have 2 particular areas that can enable developers to make things worse, specifically interfaces and abstractions (things like protected). However, FP code bases can have this same problem as well, just less common because you don’t have interfaces or protected or mutation. Either way, both require you to learn, and utilize Dependency Injection. If you’re practicing TDD, then while you’re injecting you’re dependencies, you’ll start asking yourself questions like “Dang, dude… this code requires a lot of dependencies. I don’t like testing this code. Maybe I should change the design?” which in turn leads you to Dependency Rejection; trying to make as much of the code as pure as possible (it OOP’s case that means classes/methods that have less dependencies, in FP’s case that means more pure functions).

I think the 2 sequence diagrams he shows are a wonderful visual way to show teams “This? Bad. This? Good.” Now getting from Bad to Good when you’re knee deep in bad… well, good news, beer is proof God loves us.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *