Asp.Net MVC has better unit testing support for one main reason - the whole architecture is built to use HttpContextBase
, HttpRequestBase
and HttpResponseBase
.
Asp.Net webforms is dependent on HttpContext.Current
, which is a singleton that you have no control over - it is set up and passed to your pages as part of the HttpApplication
executing the request. In most cases, in order to get the page to execute correctly, you need to execute it in a real HttpContext
. Since many of the properties of HttpContext are not settable (like Request and Response), it is extremely difficult to construct fake requests to send to your page objects.
This makes unit testing webforms pages a nightmare, since it couples all your tests to needing all kinds of context setup.
Contrast that to ASP.Net MVC, where you can mock an HttpContext! Now your code doesn't even need a web server to give it context, you can just set up the bits you need, and hand the mocked context to your method.