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

查看:114
本文介绍了使用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为null,解决了在测试设置I中创建伪造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.

如果我在真正的webrequest之外对此组件进行任何集成测试,也会发生同样的事情,是否有任何方法可以使该工作正常进行或真正模拟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?

预先告知

Fer

推荐答案

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

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

如果您要编写一个具有单个请求范围的集成测试,则应该使用singleton。

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天全站免登陆