Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I unit test my controller to make sure Windsor can resolve dependencies when using PerWebRequestLifestyle

I have the following unit test in my application:

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

        // Act
        container.Kernel.Resolve(typeof(HomeController));
    }

The point of this is to make sure I don't have any windsor configuration issues that I won't realize until I access an action on that controller. The problem is that all my object registrations are registered as PerWebRequestLifestyle so I don't get issues with my Entity Framwork Data Context being shared across web requests (which causes errors when multiple actions are run).

However, whenever I run this unit test I get the following exception:

System.InvalidOperationException: HttpContext.Current is null. PerWebRequestLifestyle can only be used in ASP.Net

How can I go about testing this scenario without changing the lifestyle of my object registration commands?

like image 395
KallDrexx Avatar asked Apr 24 '11 18:04

KallDrexx


1 Answers

I don't know if you can use the PerWebRequestLifestyle outside of ASP.NET (MVC) (I don't think you can), but you can use an IContributeComponentModelConstruction to modify the lifestyle of the components when they are registered.

This would enable you to (integration) test the container without modifying any of your Installers.

like image 79
Mark Seemann Avatar answered Oct 06 '22 01:10

Mark Seemann