MVC + Unity Containers + MSTest?

Last post 01-07-2009 1:38 PM by PaulBlamire. 3 replies.

Sort Posts:

  • Huh? [:^)] MVC + Unity Containers + MSTest?

    12-30-2008, 12:15 PM
    • Member
      5 point Member
    • ericis_com
    • Member since 05-03-2006, 9:01 PM
    • Phoenix, AZ
    • Posts 5
    How can I expose Unity to my MVC contollers for unit testing? I quickly found resources for exposing Unity to MVC through a Controller Factory in a custom HttpApplication Start event. But, unit tests usually create and call Controllers directly, so... I suppose I could create a custom Controller class that exposes and initializes Unity within a controller, but then I have the problem of running as a web application, maintaining a single instance of Unity to work with. I suppose I could attempt a static instance on some custom global object... Seems like such a kludge at this point.
  • Re: MVC + Unity Containers + MSTest?

    12-30-2008, 1:36 PM
    • Member
      368 point Member
    • maartenba
    • Member since 01-23-2008, 1:27 PM
    • Belgium
    • Posts 76

    Your unit tests will usually just instantiate a specific controller, rather than letting Unity handle this? Check www.cartrackr.net for an MVC sample using Unity + MStest.

    Visit my blog at http://blog.maartenballiauw.be

    Order my book ASP.NET MVC 1.0 Quickly via http://www.packtpub.com/asp-net-model-view-controller-1-0-quickly/book
  • Crying [:'(] Re: MVC + Unity Containers + MSTest?

    12-30-2008, 2:38 PM
    • Member
      5 point Member
    • ericis_com
    • Member since 05-03-2006, 9:01 PM
    • Phoenix, AZ
    • Posts 5

    This is a great project, but the tests are hard-coded to use a specific Repository rather than using the same logic stored within the application that relies on Unity to inject the type of Repository. This is what I am after: My logic for Unity type injection should be the same for my tests as for my application, but I should be able to configure them differently using Unity configuration sections.

    After seeing this project's implementation, I am resolved to the fact that I will need to create my own singleton for storing and managing a reference to the UnityContainer and will need to reference this object in both my tests and application.

    If done right, I suppose I could be a ridiculous developer and try to make my application IOC container agnostic; swapping out any IOC framework! An IOC for IOC frameworks? LOL! No... I have work to do.

  • Re: MVC + Unity Containers + MSTest?

    01-07-2009, 1:38 PM
    • Member
      732 point Member
    • PaulBlamire
    • Member since 09-04-2008, 7:15 PM
    • UK
    • Posts 145

    If you are trying to test your controller then I don't see what is wrong with explicitly setting the repository via the controllers constructor in your test. After all you want to test your controller, not that your controller factory is using IoC correctly.

    Re your second point see - http://www.codeplex.com/CommonServiceLocator

    http://pabloblamirez.blogspot.com - When you ask a question, remember to click "mark as answered" when you get a reply which answers your question; this ensures the right forum member gets credit below for being helpful (and makes search more relevant too).
Page 1 of 1 (4 items)