ASP.NET MVC 1.0 Release Candidate Now Available
page 14 of 20
by Scott Guthrie
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 67397/ 143

Unit Testing Improvements

The ASP.NET MVC RC includes some significant improvements to unit testing:

ControllerContext changed to no longer derive from RequestContext

The RC build includes a refactoring of the ControllerContext class that significantly simplifies common unit testing scenarios.  The ControllerContext class no longer derives from RequestContext and now instead encapsulates RequestContext and exposes it as a property.  The properties of ControllerContext and its derived types are also now virtual instead of sealed – making it significantly easier to create mock objects.

To see how this helps, let’s consider an action method like below that uses both the “Request” and “User” intrinsic objects:

Figure 34

Testing the above action method with previous ASP.NET MVC builds would have required mocking RequestContext and ControllerContext (with some non-obvious constructors that also brought in a RouteData object).

With the RC build we can now unit test it like below (using Moq to mock a ControllerContext for our Controller that allows us to simulate the Request.IsAuthenticated and User.Identity.Name properties):

Figure 35

The refactoring improvements made help out not just with testing Controller actions – but also help with testing filters, routes, custom actionresult types, and a variety of other scenarios.

View Entire Article

User Comments

Title: Awesome article, great work!   
Name: George Loew
Date: 2009-03-25 12:20:05 AM
excellent article, scott! It is very useful and contain excellent information about ASP.NET MVC framework. I have had a chance to try this ASP.NET MVC technology with my current host ( and everything works beautifully.

I am rating 5 stars for this article and I am looking forward to your next articles.
Title: Cache Photo   
Name: Meysam
Date: 2009-03-05 2:14:50 AM
Is it possible to send cache status (304) in response to subsequent requests for a photo?

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-05-25 11:20:29 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search