C# HttpContext Unit-Test - Write Testable ASP.NET MVC Web Application
ASP.NET MVC appliation is not only provided you more easy for building Web Application, it also build for runing your Unit-Test easily. So, there're two ways you could get value like, Session, Request, Response information.
- System.Web.HttpContext
- System.Web.HttpContextBase
So the differece of these two are, first HttpContext is sealed class, which means unable to override it and can't mock by using mock framework. On the other hand, HttpContextBase, is abstract class, which is able to override, so you could mock it by using mock framework too. In addition, ASP.NET MVC's controller was builded in for runing your unit test, so you don't need to do something like Dependencty Injection, to hack your HttpContextBase in to your unit test, you could just mock your HttpContextBase passing to your controller class. Following is example how to passing your mock data to your
So, once you run your unit test, it will using your mocked HttpContextBase data to runing your actual logic.
You would notice there're following you could access your HttpContext object, following is example for Session. So only the first one is old ASP.NET way of get / set session. The rest of them are able to access at Your Controller class. It's better not using the first one,
System.Web.HttpContext
Because you can't mock the data, if you want to test it, anyway, it's better to find other way to get your data, but not using the old HttpContext on your MVC.
Inconclusion, in ASP.NET MVC, you shouldn't use System.Web.HttpContext, which'll make your life hard during unit test.