I no longer work at Microsoft, so please don't bother leaving a comment here or trying to contact me through my MSDN blog.
You can find my new blog at http://www.technologytoolbox.com/blog/jjameson. My new site also provides copies of all posts from my MSDN blog.
In part 1 of this post, I provided my high-level thoughts on doing Test Driven Development (TDD) in the real world, but I didn't get around to walking through an actual sample.
To start off simple (but still real world), let's imagine we have a scenario where we need to truncate a string to a limited number of characters for display or output purposes. However, instead of just chopping off the string at the specified number of characters, we want to apply a little "intelligence" -- such as trying to break on complete words, and adding an ellipsis (...) to the end.
For example, if we pass in "Some really long string with lots of characters" and specify to truncate the string to 15 characters, then we should get back "Some really...".
[Note that if you are trying to conserve real estate on a Web page, there is actually a much better way of doing this with CSS, so please don't think of this scenario in that context.]
Applying the principles of TDD, we know we should:
So for this simple scenario, what unit test(s) should we write first?
Let's say we want to implement this functionality...