views:

784

answers:

2

How do you unit test a HttpModule in asp.net given that HttpApplication and HttpContext do no implement an interface ?

+1  A: 

In the past before moving to ASP.NET MVC, I used this library Phil Haack created for Unit Testing anything that used the HttpApplication and HttpContext. It in turned used a Duck Typing library.

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.

Dale Ragan
+2  A: 

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.

Mike
Can you point me to @Dale Ragan's article.PS: I dont have access to Twitter
chugh97
I am actually referring to the answer directly below this :)
Mike