使用 PerWebRequest 生活方式测试 Castle Windsor 组件 [英] Testing Castle windsor Component with PerWebRequest lifestyle

查看:24
本文介绍了使用 PerWebRequest 生活方式测试 Castle Windsor 组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对涉及的城堡温莎进行一些测试,在我的一项测试中,我想检查温莎安装程序,因此我检查容器是否可以根据其接口解析我的组件.

I'm trying to do some testing with castle windsor involved, in one of my tests I want to check the windsor installers, so I check that the container can resolve my components given its interface.

到目前为止,一切都很好,当组件在其安装程序中具有 PerWebRequest 生活方式时,问题就开始了,起初它抱怨 HttpContext.Current 为空,解决了在测试设置中创建假 Context 我现在有了这个nunit 测试中的异常

So far, so good, the problem starts when the component has PerWebRequest lifestyle in its installer, at first it complained about HttpContext.Current is null, having that one solved creating a fake Context in test setup I'm now having this exception in nunit test

System.Exception:您好像忘记注册 http 模块 Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule将 '' 添加到 web.config 上的部分.如果您在集成模式下运行 IIS7,则需要将其添加到

System.Exception : Looks like you forgot to register the http module Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule Add '' to the section on your web.config. If you're running IIS7 in Integrated Mode you will need to add it to section under

当我从 NUnit 运行它时,我如何在 Windsor 中注册模块或类以便它工作,或者如何模拟,因为在这个测试中并不是真正的 Web 请求,只是检查容器是否解析输入.

As I'm running this from NUnit, how I can register the module or class in windsor so it works, or how can be mocked, as in this test is not really a web request, just checking that the container resolve the type.

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

And also this same thing will happen if I make any integration tests with this component outside a real webrequest, is there any way to make this work or really mock a web request so this tests can be run?

提前排队

推荐答案

在您的测试中,您可以订阅 ComponentModelCreated 事件并将您的 per-web-request 组件的生活方式更改为其他内容.(示例).

In your test you could subscribe to the ComponentModelCreated event and change the lifestyle of your per-web-request components to something else. (example).

如果您正在编写包含单个请求范围的集成测试,则应该使用单例.

If you're writing an integration test with the scope of a single request, singleton should do.

如果您正在编写跨越多个请求的集成测试,您可以使用 上下文生活方式来模拟请求的范围.

If you're writing an integration test that spans multiple requests, you could use a contextual lifestyle to simulate the scope of requests.

包括示例中的代码(不再可用):

including code from example (which is no longer available):

container.Kernel.ComponentModelCreated += Kernel_ComponentModelCreated;

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

这篇关于使用 PerWebRequest 生活方式测试 Castle Windsor 组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆