C# 单元测试 IHttpModule
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unit testing IHttpModule
提问by Chris Porter
How do you unit test a HttpModule
in asp.net given that HttpApplication
and HttpContext
do no implement an interface ?
HttpModule
鉴于此HttpApplication
,您如何在 asp.net 中对 a进行单元测试并且HttpContext
不实现接口?
回答by Dale Ragan
In the past before moving to ASP.NET MVC, I used this libraryPhil Haack created for Unit Testing anything that used the HttpApplication and HttpContext. It in turned used a Duck Typing library.
过去,在转向 ASP.NET MVC 之前,我使用了Phil Haack 创建的这个库,用于单元测试任何使用 HttpApplication 和 HttpContext 的东西。它反过来使用了Duck Typing 库。
Unfortunately, this was the best way to do it. ASP.NET was not made to be easily testable. When they worked on ASP.NET MVC, one of the goals is to get rid of these headaches by making the framework more testable.
不幸的是,这是最好的方法。ASP.NET 并不是为了易于测试而设计的。当他们在 ASP.NET MVC 上工作时,目标之一是通过使框架更具可测试性来摆脱这些麻烦。
回答by Mike
Essentially you need to remove the HttpModule's reliance on HttpApplication and HttpContext, replacing them with an interface. You could create your own IHttpApplication and IHttpContext (along with IHttpResonse, IHttpRequest, etc) or use the ones mentioned by @Dale Ragan or use the shiny new ones in System.Web.Abstractions that are bundled with the asp.net mvc previews.
本质上,您需要移除 HttpModule 对 HttpApplication 和 HttpContext 的依赖,将它们替换为一个接口。您可以创建自己的 IHttpApplication 和 IHttpContext(以及 IHttpResonse、IHttpRequest 等),或者使用 @Dale Ragan 提到的那些,或者使用 System.Web.Abstractions 中与 asp.net mvc 预览捆绑在一起的闪亮的新的。
回答by Dror Helper
You can use an Isolation (mocking) framework. I know of two tools that enable you to fake/mock any .NET objects - Typemock Isolatorand Telerik JustMocki think that you can also use Moles.
您可以使用隔离(模拟)框架。我知道有两种工具可以让您伪造/模拟任何 .NET 对象 - Typemock Isolator和Telerik JustMock我认为您也可以使用Moles。
All of the above will enable you to fake any .NET object event if it does not implement an interface or even have a public c'tor.
如果 .NET 对象事件没有实现接口甚至没有公共 c'tor,则上述所有内容将使您能够伪造任何 .NET 对象事件。