ASP.Net MVC, Unit Testing

Model-View-Controller (MVC) is a very popular pattern for separating view state, business logic and data access concerns in an application. MVC also helps with unit testing because the pattern cleanly separates business logic from UI code and allows more of the application to be tested without having to automate the user interface.

It's always been possible to implement the MVC pattern in .Net, but there's never been guidance or tools available 'out of the box' to help with the process. The Smart Client Software Factory and Web Client Software Factory offerings help, but they're from the Patterns and Practices teams and are unfortunately not officially supported products. Recently however, Scott Guthrie announced on his blog the new official ASP.Net MVC framework. This framework is very robust and has a strong focus on unit testing. Here are a few key points (shamelessly lifted right from Scotts post):

  • It enables clean separation of concerns, testability, and TDD by default.  All core contracts within the MVC framework are interface based and easily mockable (it includes interface based IHttpRequest/IHttpResponse intrinsics).  You can unit test the application without having to run the Controllers within an ASP.NET process (making unit testing fast).  You can use any unit testing framework you want to-do this testing (including NUnit, MBUnit, MS Test, etc).
  • It is highly extensible and pluggable.  Everything in the MVC framework is designed so that it can be easily replaced/customized (for example: you can optionally plug-in your own view engine, routing policy, parameter serialization, etc).  It also supports using existing dependency injection and IOC container models (Windsor, Spring.Net, NHibernate, etc).
  • It includes a very powerful URL mapping component that enables you to build applications with clean URLs.  URLs do not need to have extensions within them, and are designed to easily support SEO and REST-friendly naming patterns.  For example, I could easily map the /products/edit/4 URL to the "Edit" action of the ProductsController class in my project above, or map the /Blogs/scottgu/10-10-2007/SomeTopic/ URL to a "DisplayPost" action of a BlogEngineController class.
  • The MVC framework supports using the existing ASP.NET .ASPX, .ASCX, and .Master markup files as "view templates" (meaning you can easily use existing ASP.NET features like nested master pages, <%= %> snippets, declarative server controls, templates, data-binding, localization, etc).  It does not, however, use the existing post-back model for interactions back to the server.  Instead, you'll route all end-user interactions to a Controller class instead - which helps ensure clean separation of concerns and testability (it also means no viewstate or page lifecycle with MVC based views).
  • The ASP.NET MVC framework fully supports existing ASP.NET features like forms/windows authentication, URL authorization, membership/roles, output and data caching, session/profile state management, health monitoring, configuration system, the provider architecture, etc.

Scott has since released an excellent 4-part series on the framework, and I highly recommend it to any ISVs planning or already doing ASP.Net development.

Comments (0)

Skip to main content