tdd pro-tip #6: prevent complex test data from spiraling out of control by going to builder & custom comparator early on.
the push-to-small, coupled with SOLID, coupled with things like third normal form, all lead us to a place of wanting to compose domain objects into potentially very rich dependency graphs.
a card in a address-tracking subsystem sounds at first blush like a class with about 5 simple strings in it, and we certainly start there when we first approach it. but as we proceed, we thicken that object quite a bit. sample thickenings: validated zip, phone, etc. support for international addresses. support for names that come in different formats than whatever your first culture uses. support for history. support for multiplicity of address, or name, or phone.
(aside: please be clear, i never start with all that crap, nor should you. guard very closely against attempting a contact card that is once and for evermore the contact card to end all contact cards. focus on your customer’s current need, one small need at a time.)
and you can see with these thickenings that our dead simple and naive first swing – a wonderful thing we shipped that we’re proud of – quickly turns in to a cluster of related objects. the simple idea of “contact” has become a directed dependency graph of many objects.
the great Gibbon wrote an astonishingly readable (from the 1770’s no less) history of the roman empire, and he frequently used the word “insensibly”, which wasn’t a word i was familiar with. the word just means “all unawares”. i slid insensibly towards a fondness for deviled eggs just means that i did so without ever even noticing how attached to them i was becoming. [ed. note: please do not send him more deviled eggs. it’s just gross, now.]
we slide the objects we create insensibly towards object-clusters all the time. the sliding itself is awesome. the part where it’s insensible, tho, that’s death to your geek performance.
why? two reasons. 1) data setup, 2) test validation. they both get inch by inch more difficult as we thicken our objects. and we sit there, face all covered with that awesome mustard-y yellow crumb stuff, not even noticing that we’ve just incited a monstrous belly-ache.
so. if my code has to tweak some part of a thickened object, my test has to either make that thickened object or make that sub-object. if it’s just the sub-, great, we’re good to go. but if the owning object has functions and those functions depend on the sub-object’s behavior, it gets a little messier. now i’m making the owning object, and all its subs. and i’m testing the owning object, and all its subs.
if you’ve been inhaling programming theory, and you should be, you’ll point out that were the owner and the component well-factored, this won’t happen. and that to me seems like an entirely accurate statement, tho someone may give me counter-examples.
but remember, we work for a living. and if we’re any good at it at all, we work for a living solving problems we haven’t solved before. that means we’re constantly introducing new function, new classes, new everything. programming happens in time. not only is it low odds that my new code is well-factored in the beginning, it’s also low odds that the existing code i’m adding to is well-factored for my addition in the beginning. the well-factoredness of the code is just not a thing i can wait for before i start.
so, back to the tip. builders and custom comparators. a builder is just a class or framework that lets you make new objects without fully describing them. a custom comparator is the same, except it lets you compare two objects without caring about certain aspects of them.
when i am changing how the contact interacts with its phone field, i am utterly and completely indifferent to how it interacts with its contact type, its address, its history, its editor, its – it goes on an on. that indifference means a) i don’t want to specify all that crap when i make a contact, and b) i don’t want to compare all that crap when i write assertions.
solution: a generic one-size-fits-all-test way to specify a default contact and override just the parts you want. a custom one-size-fits-my-current-tests way to specify the comparisons you care about.
if the contact is a simple object, we can usually get away with some sort of default value scheme, either using literally default arguments, alternate constructors, or a helper function in our test that holds them all steady. all of these are good if they do the trick. when contact becomes thick enough, tho, we have not only sub-objects in our fields, but often sub-sub-objects, too. here, the simple approaches above just won’t cut it.
the case is similar with comparators. early on, even when we write custom comparators, they can be simple one-size-fits-test, and work like a charm. when things get thick enough, tho, it’s likely to get owwie.
so, i’m already going on too long, what to do?
start by learning what builders do and how.
next learn any handy cool language support u might have for them, like fluency or extension methods.
third, learn how to write comparators and make them do anything you want.
my recent case on this is already mentioned, building golden datasets that pretend to be my app’s variety of upstream data sources. the datasets are richly cross-connected. specifying those directly is nearly impossible w/o a builder. it’s dead easy with one.
the builder interface i use lets me not just supply the cross-linked data, but to express it many times more simply than the app itself (or the live upstreams) do. tests go from having huge setups to tiny ones, from having lots of noise to “only important words used”.
the deepest part of this pro-tip? your tests must be tended exactly as much and exactly the same as your shipping code. as soon as they tricky to write, wonder about investing a few hours in untricky-ing them.
don’t let insensible complication creep in to your code, of course, but don’t let it creep in to your tests, either.