Auto-mocking container pitfalls?

I’m taking a closer look at the auto-mocking container idea, specifically as we’re including it in the upcoming release of NBehave.  I’m a little wary of prolonged use, but wanted to get some feedback (it’s also on the ALT.NET message board).  Some pitfalls I thought of offhand were:

  • Can allow dependencies to get out of hand
  • Can hide a code smell where you have too many dependencies
  • Forces a reliance on an IoC container for all creation, inside and outside of tests

Specifically the one I’m most worried about is making it too easy to add a bunch of dependencies and creating god-classes that have too much responsibility and coordinate too much.  If I hide that complication in a unit test, it might be perpetuating a bad design.

On the other hand, plenty of design patterns exist to help hide that problem, including creation methods, factory methods, factories and abstract factories.  But I’m thinking I might rather have those abstract away the “what and how” than “just too many dependencies”.

Finally, AMC could be another sharp tool in the toolbox.  It can cut you if you’re inexperienced, but sometimes it can make the job much easier.  Luckily, no mistake is ever more than an iteration’s length away from reversal.  I’m going to proceed carefully, mindful of the pitfalls I think I might land in.

About Jimmy Bogard

I'm a technical architect with Headspring in Austin, TX. I focus on DDD, distributed systems, and any other acronym-centric design/architecture/methodology. I created AutoMapper and am a co-author of the ASP.NET MVC in Action books.
This entry was posted in TDD, Tools. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • @Jimmy – What did you mean by the third point? You could not use an IoC in production code, but still use an AMC in tests. I don’t know why you would do that, but you could. Maybe I didn’t understand the point?

  • @Ray

    I’m just talking about a forced reliance on an IoC container. If you don’t use an IoC container in your tests, it forces you to think of the design from the client’s perspective. If you never, ever construct the component anywhere, you’re not really seeing the client’s view.

  • @Jimmy – ah, I see what you’re saying. I think if you use AMC you should just handle with care like you said.