Unit testing 未注册在httpModules中测试nhibernate Castle Windsor映射

Unit testing 未注册在httpModules中测试nhibernate Castle Windsor映射,unit-testing,asp.net-mvc-2,mstest,Unit Testing,Asp.net Mvc 2,Mstest,我想编写一个测试来验证castle windsor中的映射。 我正在使用ASP MVC2,我正在使用castle windsor映射我的存储库 我读过这篇文章: 基于此,我创建了我的MS测试 [TestMethod()] public void GetContainerTest() { MooseMvc.Infrastructure.DependencyInjectionInitialiser target = new MooseMvc

我想编写一个测试来验证castle windsor中的映射。 我正在使用ASP MVC2,我正在使用castle windsor映射我的存储库

我读过这篇文章:

基于此,我创建了我的MS测试

 [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);
            }
        }
target.getcontainer()的数据实现

 this._windsorContainer.Register(Component.For<TInterfaceType>()
                .ImplementedBy(typeof(TConcreteType)).LifeStyle.PerWebRequest);
this.\u windsorContainer.Register(Component.For())
.ImplementedBy(typeof(TConcreteType)).lifety.PerWebRequest);
我收到如下消息:

 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>
看起来您忘记注册http模块了
Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule将“”添加到web.config上的部分。
如果在集成模式下运行IIS7,则需要将其添加到
下节

我找到了一本漂亮的指南


没有什么可添加的了。

我遇到了同样的问题,我找到了一个解决方案:您可以在单元测试的构造函数中定义一个事件来覆盖Lifestyle类型

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);  
}

谢谢这正是我所需要的。哈哈,几个月后,我在另一个项目中遇到了同样的问题,它再次为我解决了这个问题。谢谢PS只需记住在任何安装程序或注册调用之前进行ComponentModelCreated事件注册!