I’ve been practicing and advocating TDD (Test Driven Development) even before I’ve started working at Typemock but I can point at a specific point of time in which I actually “got it”.At that time I had a great mentor and I was sure I got the whole “Red-Green-Refactor” routine, In fact I knew it so well that I allowed myself to “speed development” by writing the code before the actual test. One day while happily coding with a fellow developer we came across an interesting problem: we needed to create a value generator – a class that will return a unique value each time a method (GetNextValue) is called. Of course being two bright and talented developers we’ve started by debating how this class should be implemented and so that it would support every conceivable type – needless to say after a few minuets we were still “designing” and every design we had was flawed – it had a bunch corner cases that forced us to search for yet another better-stronger-faster design. Luckily for us we had someone in the same room that saw our plight and decided to put a stop to it. What he did is remind us how TDD should be done – one test at a time.“Write a test that checks two unique integers” – he said. “But it won’t work for strings or even doubles” – we said.“Do it anyway” - And we did:[TestMethod]public void ValueGenerator_GenerateValuesForInt_TwoDifferentValuesReturned()
{
var val1 = ValueGenerator.GetNextValue(typeof(int));
var val2 = ValueGenerator.GetNextValue(typeof(int)); Assert.AreNotEqual(val1, val2);
}Read more: Helper Code
{
var val1 = ValueGenerator.GetNextValue(typeof(int));
var val2 = ValueGenerator.GetNextValue(typeof(int)); Assert.AreNotEqual(val1, val2);
}Read more: Helper Code
0 comments:
Post a Comment