Roy Osherove

View Original

Partial Classes - Add to your Unit Testing Bag of Tricks

Ralf has a nice tidbit about how .NET 2.0 Partial Classes can help you with writing unit tests for methods on classes that are otherwise harder to get to.
It's a nice trick to add to the trick bag, though I'd argue that if you were doing Test-Driven-Development in the first place, you'd have a testable design out-of-the-box, and you wouldn't need to come up with clever hacks such as these.
Having said that, sometimes you just don't have a choice and you need to write tests for legacy code (a.k.a "code that works"), and in that case, this just might be a lifesaver.
 
Another lifesaving tool to use with your legacy code would be TypeMock. I recommend you take a long hard look at that little miracle of a tool (which was recently transferred into Israeli ownership, BTW) that can help you overcome lots of legacy code coupling issues in your tests. I should do a whole article on that when I get the time..
Cross Posted from Blogs.Microsoft.Co.Il/Blogs/Royo