Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing nhibernate Castle Windsor mappings in httpModules is not registered

I want to write test that verifies mappings in castle windsor. I am using ASP MVC2 where i am using castle windsor to map my repositories.

I have read this article:

http://weblogs.asp.net/bsimser/archive/2008/06/04/the-first-spec-you-should-write-when-using-castle.aspx

and based on this i have created my MS Test

 [TestMethod()]
        public void GetContainerTest()
        {
            MooseMvc.Infrastructure.DependencyInjectionInitialiser target = new MooseMvc.Infrastructure.DependencyInjectionInitialiser(); // TODO: Initialize to an appropriate value
            IWindsorContainer container = target.GetContainer();
            foreach (IHandler assignableHandler in container.Kernel.GetAssignableHandlers(typeof(object)))
            {             
                container.Resolve(assignableHandler.ComponentModel.Service);
            }
        }

The data for target.getcontainer() implements

 this._windsorContainer.Register(Component.For<TInterfaceType>()
                .ImplementedBy(typeof(TConcreteType)).LifeStyle.PerWebRequest);

I get message as follows:

 Looks like you forgot to register the http module 
Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule Add '<add
name="PerRequestLifestyle"
type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule,
Castle.Windsor" />' to the <httpModules> section on your web.config.
If you're running IIS7 in Integrated Mode you will need to  add it to
<modules> section under <system.webServer>
like image 482
cpoDesign Avatar asked Jan 06 '12 09:01

cpoDesign


2 Answers

I have had the same problem and I have found a solution: You can define an event in the contructor of the unit test to override the LifestyleType.

void Kernel_ComponentModelCreated(Castle.Core.ComponentModel model)
{
    if (model.LifestyleType == LifestyleType.Undefined)
        model.LifestyleType = LifestyleType.Transient;

    if (model.LifestyleType == LifestyleType.PerWebRequest)
        model.LifestyleType = LifestyleType.Transient;
}

public UnitTest1()
{
    containerWithControllers = new WindsorContainer();

    containerWithControllers.Kernel.ComponentModelCreated += new ComponentModelDelegate(Kernel_ComponentModelCreated);  
}
like image 128
Jordi Avatar answered Sep 27 '22 17:09

Jordi


i have found beautifull guide

http://docs.castleproject.org/Windsor.Windsor-tutorial-part-three-a-testing-your-first-installer.ashx

there is not much else to add..

like image 35
cpoDesign Avatar answered Sep 27 '22 18:09

cpoDesign