title: Setup Unit Test to test a controller using Unity
body: <p>I use Unity to create an instance of <code>CustomerService</code>. That's work for the code below in the ASP.NET MVC application.</p>
  
  <p>When I create the ASP.NET MVC application I created the ASP.NET MVC test project. I'd like test the actions (here : <code>/Home/Index</code>).
  The problem is I can't set <code>Unity</code>. I'd like create a real call to the action and no mocking.</p>
  
  <p>Do you have an idea how setup in the test project ?  I tried this but I don't find the right syntax to create the instance of <code>CustomerService</code> to use as parameter when I call the controller.</p>
  
  <pre><code>[TestMethod]
  public void Index()
  {
      var container = UnityConfig.GetConfiguredContainer();
  
      //I don't find the right syntax for resolve
      ICustomerService customerService = container.Resolve??????
  
      HomeController controller = new HomeController(customerService);
      ViewResult result = controller.Index() as ViewResult;
      Assert.IsNotNull(result);
      container.Dispose();
  }
  </code></pre>
  
  <p>Working code used in ASP.NET MVC application</p>
  
  <pre><code>namespace MyTestMVC
  {
      public class HomeController : Controller
      {
          private readonly ICustomerService _customerService;
  
          public HomeController(ICustomerService customerService)
          {
              _customerService = customerService;
          }
  
          public ActionResult Index()
          {
              var result = _customerService.MyMethod();
              //.....
              return View();
          }
      }
  }
  
  //I set Unity like this : 
  namespace MyTestMVC.App_Start
  {
      public class UnityConfig
      {
          private static Lazy&lt;IUnityContainer&gt; container = new Lazy&lt;IUnityContainer&gt;(() =&gt;
          {
              var container = new UnityContainer();
              RegisterTypes(container);
              return container;
          });
  
          public static IUnityContainer GetConfiguredContainer()
          {
              return container.Value;
          }
  
          public static void RegisterTypes(IUnityContainer container)
          {
              container.RegisterType&lt;ICustomerService, CustomerService&gt;();
          }
      }
  }
  
  [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(MyTestMVC.App_Start.UnityWebActivator), "Start")]
  [assembly: WebActivatorEx.ApplicationShutdownMethod(typeof(MyTestMVC.App_Start.UnityWebActivator), "Shutdown")]
  namespace MyTestMVC.App_Start
  {
      public static class UnityWebActivator
      {
          /// &lt;summary&gt;Integrates Unity when the application starts.&lt;/summary&gt;
          public static void Start() 
          {
              var container = UnityConfig.GetConfiguredContainer();
              FilterProviders.Providers.Remove(FilterProviders.Providers.OfType&lt;FilterAttributeFilterProvider&gt;().First());
              FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));
              DependencyResolver.SetResolver(new UnityDependencyResolver(container));
          }
  
          public static void Shutdown()
          {
              var container = UnityConfig.GetConfiguredContainer();
              container.Dispose();
          }
      }
  }
  </code></pre>