Some of the detractors of TDD or testing in general is that you waste time writing test instead of writing “useful” code.
Well, writing test just save me a few hours of debugging. Look at this very simple class, there is an stupid mistake in the code.
Did you see it? Good for you! I didn’t but since I have my test and when I run it it fail.
Notice that the error mention that widget.Template.Rendered was null, but I’m mocking the ICache implementation and returning the template string when Get is called with the right key.
Upon running the test in debug the error was evident, I was calling getTemplateView before setting the Key property of the object!
Here is the code after been fixed, changing the position of lines 15 and 16 did it.
The good thing is that I caught the error immediately, while the logic that I use to write the code was still fresh in my mind, so I did not spend hours trying to figure out what’s going on. Granted this is a very easy to spot issue, but believe that if you catch this error three or four days after writing the code you will spend more than the 5 minutes that took to write the unit test.