Table of Contents – Adding Tests – Again
If you haven’t yet read this chapter yet, then buckle up. It’s purposely long, as Adam indicates that he didn’t go into a good amount of detail in the last take-an-existing-function-and-add-tests-to-it example/chapter. With that, let’s discuss.
The first few sections we’re actually quite helpful. The whole thing was; don’t get me wrong. But these sections, more or less discussed something that’s important to me, and that’s using functions, and ensuring that they’re small and single purposed. It wasn’t word-for-word, but I’ve often said and written that I’d rather troubleshoot five 20 line functions, than one, one hundred line script. The book and I weren’t exact on words; however, we were on meaning. It’s much easier to handle things in small amounts. Think, checkbook registers — for those still using one — it’s much easier to enter things in small batches, as you spend your money, than to save up all your receipts until the end of the month, and then enter them. So seriously, let’s shoot for function writing only, and making them small and simple.
We also discussed the relation of the number of parameters to that of the number of tests. It makes sense to me, as each will add more ways in which the code will need to be tested. We covered using various types of parameter validation to limit the number of tests, as well, and then using helper functions to remove some of a function’s required, in-function work. We took one function and made three, and in doing so, made testing — and even supporting it — an easier thing to do. At first glance, I wasn’t sure there was that much we could alter; the function looked good to me, and yet we did remove some work from it, to be shared across helper functions — it was good to see.
Once we had our slimmed down function, we discussed flow, code paths, creating tests for the shortest code path, as well as other code paths, and saw some great examples of mocking. We’re still working with unit testing, and so you’ll appreciate the mocking examples in this chapter, if you’d like to see more. I did. Like the last one, this chapter helps make this book. While I enjoyed learning the basics from Part I, this has gotten much more interesting, to no surprise.