开发者

How can I unit test that Windsor can resolve Asp.net MVC3 controller dependencies?

Currently I have Windsor controller factory that is working well. However, I am changing my controllers a bit and having Windsor instantiate more data for controllers. I forgot to update my Installer class and it failed.

Thus, I realized this is a perfect instance for unit testing, so every time I run my unit tests it will verify that Windsor is set up correctly.

Thus I created the following unit test:

    [TestMethod]
    public void Windsor_Can_Resolve_HomeController_Dependencies()
    {
        // Setup
        WindsorContainer container = new WindsorContainer();

        // Act
        HomeController 开发者_如何学JAVAcontroller = (HomeController)container.Kernel.Resolve(typeof(HomeController));
    }

This is exactly the same code that exists in my WindsorControllerFactory.GetControllerInstance() method so I am not sure why this isn't working. When this runs I get:

Test method MyApp.Tests.Controllers.HomeControllerTest.Windsor_Can_Resolve_HomeController_Dependencies threw exception: 
Castle.MicroKernel.ComponentNotFoundException: No component for supporting the service MyApp.Controllers.HomeController was found

I'm not sure how to tackle this. The only thing I can think of is that since it's in my test project, and not my Asp.net MVC project it might not be automatically picking up my CommandAndQueryInstaller class, which contains all of my windsor type registration.

Any advice for unit testing windsor dependency configuration?


You are creating a new WindsorContainer in your setup and not registering any dependencies in the container. You'll need to run your IWindsorInstallers in order to populate the container.

[TestMethod]
public void Windsor_Can_Resolve_HomeController_Dependencies()
{
    // Setup
    WindsorContainer container = new WindsorContainer();
    container.Install(FromAssembly.Containing<HomeController>());

    // Act
    HomeController controller = container.Resolve<HomeController>();
}

Note that you don't have to access the kernel. You can use container.Resolve<T>(), which also takes care of the cast internally.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜