Lesson 5: Extract complex conditionals and test them in isolation
There’s a tension here I’ve not seen many people talk about. In order to test an extracted conditional in isolation you need to make it exportable from your module. But that increases the “public API” of your module, which encourages more coupling. But if you keep the function private to the module then you can’t test it.
At least in the Ruby world people seem to err on the side of not testing private methods and only testing public behavior. I wonder if this is a sign we need different testing techniques.
I was going to say ‘Property based testing is pretty good for this!’ and then promptly realised who I was replying to..
In any case, Property Based Testing /is/ in my view a good fit for this. Taking the example from the article, the invariants of the system are basically if you’re in the USA and less than 21, you shouldn’t be able to purchase a beer, ever. To me that feels actually pretty simple to express using the aforementioned technique.
There’s a tension here I’ve not seen many people talk about. In order to test an extracted conditional in isolation you need to make it exportable from your module. But that increases the “public API” of your module, which encourages more coupling. But if you keep the function private to the module then you can’t test it.
At least in the Ruby world people seem to err on the side of not testing private methods and only testing public behavior. I wonder if this is a sign we need different testing techniques.
I was going to say ‘Property based testing is pretty good for this!’ and then promptly realised who I was replying to..
In any case, Property Based Testing /is/ in my view a good fit for this. Taking the example from the article, the invariants of the system are basically if you’re in the USA and less than 21, you shouldn’t be able to purchase a beer, ever. To me that feels actually pretty simple to express using the aforementioned technique.