Saturday, August 12, 2006

TypeMock - Helps Improve Your Designs?

If anyone ever reads this blog, and if they've used TypeMock, then I'd be interested in what they thought of it. For those of you who haven't used it you should take a look if your doing TDD as it allows you to use mocking without redesigning your code.

For me the question is whether designing your code for testability is actually a good idea, if so using TypeMock too much is a bad idea.

Personally I'm much in favor of TDD but I think I disagree with most of the books/Websites because I don't think that decoupling your code to allow mocking necessarily results in particularly good designs.

Say I want to test a class that relies on a concrete Foo class . One way to get a mock Foo in for testing would be to create an IFoo interface, implemented by Foo and a new MockFoo class. To get the MockFoo into the class being tested I'd use something like IOC or a factory. An alternative would be to use NMock to create a dynamic mock for Foo, again its up to me to get the mocked object into the class being tested.

In the past I've tried doing this sort of decoupling for testability but my view is now that it adds unnecessary complexity. I also found that the interfaces/designs I was producing didn't necessarily decouple the software in ways that were later useful.

Thats where I think TypeMock is strong, I can use TDD to design the interfaces of my classes for normal usage and use TypeMock to mock out depedencies of the class being tested. Then once my class is in real usage I can look to decouple when and if its needed.

Share This - Digg It Save to Stumble It! Kick It DZone


  1. Brian5:06 pm

    Looking at TDD, I've been struggling with the same question. I think the tradeoff is readability vs. extensability

    If you expect the code to be extended with "plugins" the extra level of indirection is great. For infrastructure-type projects such as log4net this works really well.

    However, in enterprise applications I've found that "extensability by plugin" is not often required in the domain or presentation layers. So the application does not change in ways that the extra interfaces/DI help - in fact they reduce readability becoming a hinderance.

    My feeling right now is, if we build unit tests with something like TypeMock, we have an application that supports change (via the unit test safety net) while at the same time not sacrificing readability.

  2. I completely agree, we've been using this approach for quite a while now and so far it has worked a treat.

    Another advantage is that in the places where we have used interfaces/abstract base classes it stands out to the reader.