today, let’s talk a little about the chaining premise, from five underplayed tdd premises.
the chaining premise says “test a chain by testing its links”. like the other premises, it’s easy to make it pithy, but it has vast ramifications about when we’re doing TDD.
when we talked about the money premise, i gave a long, likely partial, list of ways TDD supports that premise. did you notice i never mentioned the customer? TDD is for developers. the people it helps the most are the geeks who do it. (oh, don’t worry. TDD works for developers, but remember, developers work for managers, and managers work for customers. it all works out in the end, i promise.)
for the TDD tests to make us ship more value faster, they have to be “cheap”. i’ll define that vague word using some other vague words. ”cheap” means “easy-to”. TDD tests want to be easy to … scan, read, write, change, run, and diagnose. that means – remember the thinking knee – they have to be small.
consider an app. it consists of, in decreasing order of size: systems, subsystems, programs, layers, packages, objects, and functions. all arranged in an intricate directed dependency graph. A calls B calls C calls D calls, well, it’s dependencies all the way down. some apps are huge. most apps are just “bigger than a breadbox”. a small number of apps are small.
how are we going to write small tests against large apps? this is where the chaining premise steps in.
the chaining premise says 1) we can test by testing only parts of the app at one time, and 2) the natural parts are the arrows in that A->B->C->D dependency chain, and 3) the cheapest tests work pair-by-pair along the chain. a possible confusion: it’s not really a chain. dependency graphs are directed graphs, and any given part might depend on more than one other part. normally, A willl depend on B and C, each of which have their own dependencies.
call a “unit under test” one of these letters, A. say that A depends on, uses, imports, B, C, and D. we’ll say that A “collaborates” with B, C, and D, and that they are A’s “collaborators”. to test that A does what the geek thinks it does – the heart of TDD – we hook A up to its collaborators, give it some commands, and poke around a little to see if A did the thing we thought it did.
okay, but wait. isn’t it true that if I hook A up to a collaborator, i’m hooking it up to all of that collaborator’s dependencies, and so and so on all the way down?
fine. we’ll trick the A. we’ll give it a thing that it thinks is its collaborator, but is really just a simulator!! MY GOD, WE’RE GONNA BE RICH!!! except. wait. if you can write a simulator for a collaborator that does everything that collaborator does in every possible circumstance, aren’t you going to wind up with just as many dependencies?
are we stumped? is the chain premise now dead? sure feels like a lot of damned thinking for nothing. ya know, mama dint raise no thinkers.
so ya got these tests and you need – oh. that’s it, that’s the problem, we’re thinking of all these tests. but we only actually write, or for that matter scan/read/run/change/diagnose one test at a time. we don’t need a big heavy simulator, we need a bunch of really stupid fakes. really stupid fakes are much easier than simulators. if we can find ways to do this quickly and easily, we’re back on track.
so this chain premise has us testing A by testing one path through one function at a time, supplying really stupid fakes instead of real collaborators.
a contrived but demonstrative example. in order to decide what to do with a notification, say that A needs to know whether it’s business hours or not. it uses an Hours for this. it says “if(hours.isCurrentlyOpen()) … else …“ we write one test for the if, and another one for the else. we don’t use a real Hours for either test. for the first one, we have an Hours that always says it’s currently open. for the other, the opposite.
notice that this trick breaks the dependency tree. A still depends on Hours, but the Hours it depends on doesn’t depend on anything else. this means i am testing just one piece of the chain, the A. i’m not testing its collaborators, i’m not testing the app, i’m testing A.
and what, after all, are we testing here? we are testing that A works the way we thought it did ASSUMING that its collaborators work the way we thought they did. a person might wonder how much that’s worth. how much is it worth to know that a given piece works assuming the pieces it talks to work? the answer is that it’s worth way more than the cost of doing it.
or, anyway, that’s what the chain premise says. “test a chain by testing each link”. if we can cheaply know for every A, B, C, … that it works if what it depends on works, i have gone a fabulous distance towards knowing that the whole thing works.
time for some provisos and caveats.