Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing IHttpModule

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

like image 705
Chris Porter Avatar asked Aug 24 '08 18:08

Chris Porter


2 Answers

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.

like image 81
Mike Avatar answered Nov 02 '22 15:11

Mike


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.

like image 2
Dale Ragan Avatar answered Nov 02 '22 15:11

Dale Ragan