首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PerWebRequest lifestyle测试Castle windsor组件

使用PerWebRequest lifestyle测试Castle windsor组件
EN

Stack Overflow用户
提问于 2011-04-26 02:20:57
回答 4查看 3.8K关注 0票数 19

我正在尝试做一些测试,在我的一个测试中,我想检查windsor的安装程序,所以我检查容器是否可以解析给定的接口的组件。

到目前为止,一切顺利,当组件在其安装程序中有PerWebRequest lifestyle时,问题就开始了,一开始它抱怨HttpContext.Current为空,已经解决了在测试设置中创建假上下文的问题,现在我在nunit测试中遇到了这个异常

http :看起来你忘记注册Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule模块了,把'‘添加到你的web.config部分。如果在集成模式下运行IIS7,则需要将其添加到

当我从NUnit运行时,我如何在windsor中注册模块或类以使其工作,或者如何被模拟,因为在这个测试中并不是真正的web请求,只是检查容器是否解析了类型。

同样的事情也会发生,如果我在一个真实的web请求之外使用这个组件进行任何集成测试,有没有什么方法可以让它工作,或者真正模拟一个web请求,以便可以运行这些测试?

提前完成任务

Fer

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-04-26 03:19:11

在您的测试中,您可以订阅ComponentModelCreated事件,并将每个web请求组件的生活方式更改为其他组件。(example)。

如果您正在编写具有单个请求范围的集成测试,则singleton应该可以。

如果您正在编写跨多个请求的集成测试,则可以使用contextual lifestyle来模拟请求的范围。

编辑:包含示例中的代码(不再提供):

代码语言:javascript
复制
container.Kernel.ComponentModelCreated += Kernel_ComponentModelCreated;

代码语言:javascript
复制
void Kernel_ComponentModelCreated(Castle.Core.ComponentModel model)
{
    if (model.LifestyleType == LifestyleType.Undefined)
        model.LifestyleType = LifestyleType.Transient;
}
票数 21
EN

Stack Overflow用户

发布于 2019-06-19 14:51:05

从Windsor的版本5开始,如果你使用的是Castle.Facilities.AspNet.SystemWeb.WebRequestScopeAccessor,那么公认的答案就不起作用了,因为PerWebRequest的生活方式已经是一种有范围的生活方式。

我通过将ComponentModelCreated委托更改为以下内容来使其正常工作:

代码语言:javascript
复制
void Kernel_ComponentModelCreated(Castle.Core.ComponentModel model)
{
    const string CastleScopeAccessorType = "castle.scope-accessor-type";

    if (model.ExtendedProperties.Contains(CastleScopeAccessorType))
    {
        model.ExtendedProperties.Remove(CastleScopeAccessorType);
    }
}
票数 1
EN

Stack Overflow用户

发布于 2020-04-24 03:37:05

我最终实现了这个扩展。提示:必须在加载具有PerWebRequest lifestyle的组件之前调用

代码语言:javascript
复制
public static class WindsorContainerExtensions
{
    public static IWindsorContainer OverridePerWebRequestLifestyle(this IWindsorContainer container)
    {
        container.Kernel.ComponentModelCreated += model =>
        {
            if (model.IsPerWebRequestLifestyle())
            {
                model.LifestyleType = LifestyleType.Transient;
            }
        };

        return container;
    }

    private static bool IsPerWebRequestLifestyle(this ComponentModel model)
    {
        return model.LifestyleType == LifestyleType.Scoped
            && model.HasAccessorType(typeof(WebRequestScopeAccessor));
    }

    private static bool HasAccessorType(this ComponentModel model, Type type)
        => model.HasExtendedProperty("castle.scope-accessor-type", type);

    private static bool HasExtendedProperty<T>(this ComponentModel model, object key, T expected)
    {
        return model.ExtendedProperties[key] is T actual
            && EqualityComparer<T>.Default.Equals(actual, expected);
    }
}

需要以下导入:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using Castle.Core;
using Castle.Facilities.AspNet.SystemWeb;
using Castle.Windsor;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5781599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档