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

I currently have a Windsor factory controller that works well. However, I am changing my controllers a bit, and Windsor is creating more data for the controllers. I forgot to update my installer class and it failed.

Thus, I realized that this is an ideal example for unit testing, so every time I run my unit tests, it checks that Windsor is configured correctly.

So I created the following unit test:

[TestMethod] public void Windsor_Can_Resolve_HomeController_Dependencies() { // Setup WindsorContainer container = new WindsorContainer(); // Act HomeController controller = (HomeController)container.Kernel.Resolve(typeof(HomeController)); } 

This is exactly the same code that exists in my WindsorControllerFactory.GetControllerInstance() method, so I'm not sure why this is not working. When this is done, 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 am not sure how to handle this. The only thing I can think of is that since in my test project, and not in my MVC Asp.net project, it cannot automatically build my CommandAndQueryInstaller class, which contains all my registration like a windsor.

Any tips on setting up dependencies for wind module testing?

+4
source share
1 answer

You create a new WindsorContainer in your setup and do not register any dependencies in the container. To fill the container, you need to run your IWindsorInstallers.

 [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 do not need to access the kernel. You can use container.Resolve <T> (), which also takes care of casting inside.

+5
source

Source: https://habr.com/ru/post/1347319/


All Articles