2012-01-06 2 views
7

Voglio scrivere test che verifica i mapping in windsor del castello. Sto usando ASP MVC2 dove sto usando windsor del castello per mappare i miei repository.Testing nhibernate Castle Windsor mapping in httpModules non è registrato

Ho letto questo articolo:

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

e sulla base di questo ho creato il mio 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); 
      } 
     } 

I dati per target.getcontainer() implementa

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

Ottengo il messaggio come fol bassi:

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> 

risposta

2

ho avuto lo stesso problema e ho trovato una soluzione: È possibile definire un evento nel contructor del test unitario per sovrascrivere 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); 
} 
+0

Grazie! Esattamente quello di cui avevo bisogno. –

+0

Haha, alcuni mesi dopo vengo a questo stesso problema in un altro progetto e lo risolve di nuovo per me. Grazie! PS Ricorda solo di fare questa registrazione dell'evento ComponentModelCreated prima di ogni chiamata di Installer o Register! –