I have been using TDD for several years now and I have found that it manages the design of the API for a more convenient design, giving you two different clients for the API from the start; you have production code and test code, both of which want to manage the API in different ways.
It’s true that sometimes I add things to simplify the testing of the API, but I almost always think that the things that I think I just use for testing are actually very useful for monitoring purposes. So, for example, FooAllocator may end with an optional constructor argument, which is the monitoring interface ( IMonitorFooAllocations ), which is very useful for bullying during testing so that I can look inside, but which also tends to be very useful when you suddenly find that you need set some distribution indicators for the rest of the world during production. Now I tend to think about additional bits that I can add to provide easy testing in terms of their dual use for additional product monitoring. I generally write server code and can expose internal things, as perfmon counters are VERY useful ...
Similarly, you correctly say that often the objects that make up the API can take some other objects explicitly and not stretch them and get them from a known place, but that's good. Believe me, once you get used to explicit dependencies, you won’t want to go back to digging a class after a class to find out how and why your Widgets accesses the active directory when there is no hint in the API that they want to do this. It also often happens that you break these dependency relationships during design and testing, and then hide them again when you connect all the parts. You are still “parameterizing from above”, but more often than not, the API object model can mean that you never see “above” as an API user. You get one place to configure the API with the things that it needs, and often this is no different from what it would look like if you had a lot of singles and globals and hidden dependencies.
But remember, TDD is a tool when it does not fit, do not use it.
Len Holgate Dec 08 '09 at 17:03 2009-12-08 17:03
source share