I have just started to learn ASP.NET MVC. When comparing ASP.NET MVC with Web Forms one of the main advantage of MVC is always told to be better support of Unit Testing. Can I got a good explanation of how it has better support?
Edit :
If possible please provide example in both.
Asp.Net MVC has better unit testing support for one main reason – the whole architecture is built to use
HttpContextBase,HttpRequestBaseandHttpResponseBase.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 theHttpApplicationexecuting the request. In most cases, in order to get the page to execute correctly, you need to execute it in a realHttpContext. 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.