Coding: Paying attention
Jeremy Miller tweeted earlier in the week about the dangers of using an auto mocking container and how it can encourage sloppy design:
That whole "Auto Mocking Containers encourage sloppy design" meme that I blew off last week? Seeing an example in our code.
I haven’t used an auto mocking container but it seems to me that although that type of tool might be useful for reducing the amount of code we have to write in our tests it also hides the actual problem that we have - an object has too many dependencies.
By hiding the creation of stubs/mocks for those dependencies in our test we are addressing the effect and not the cause i.e. we are bear shaving.
Jeremy followed this up with a couple of quite insightful comments:
You know though, I still think it comes down to you being responsible for paying attention.
It’s no different than still having to worry about DB optimization even though the ORM is shielding you from the details
Another somewhat related situation where I’ve noticed a similar problem is when we have several tests which require a certain method to be stubbed out and in the interests of reducing duplication we pull that up into a setup method.
While this achieves that goal it also means that there is information that is hidden away from us when we read each of our tests.
One approach that I’ve seen encouraged is that we should never use a setup method so that we have to create everything we need for our test in the test body.
I quite like this approach because it encourages us to see any problems that we’re creating with respect to writing difficult to test code but quite often what ends up happening is we’ll copy and paste tests because there’s more code to write for each test.
I’m coming to the conclusion that there’s no one approach that will stop us making design mistakes and that as Jeremy says, we need to make sure that we pay attention to what we’re doing.
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.