Exploring the ASP.NET MVC Preview 2 – First Glance
Download(s): MvcMockHelpers rev. 3
Better late than never, I suppose. Between my home computer going on the fritz and the BEAUTIFUL weather making me want to just sit outside, this article has taken me nearly three days.
A few months ago, I wrote a series of articles evaluating Preview 1 of the MVC Framework with rave results. After working with Java for a while, I was thrilled to see MVC patterns hit .NET WITHOUT the sometimes painful manual implementations that I had been using to-date.
Preview 2 builds on a few of those expectations, fixes some bugs, addresses a bit of testability, and, generally, just works.
After installing Preview 2, you’ll notice that the project templates have changed around a bit. Thankfully, they’ve separated out the MVC Web Application and MVC Test templates into their correct project types classifications. What’s even better? When adding a MVC Web Application, it now PROMPTS you to build in your tests.
Important: I’d like to point out that the “Create Test Project” dialog only shows up when you are initially adding the ASP.NET MVC Web Application. If you choose “No, do not generate a unit test project” and later try to add a new MVC Test Project, you will not have the option to select the testing framework. I truly hope this changes in future builds. 🙂
Another great feature is that the ‘Test Framework’ is fully configurable. The folks at MbUnit have already built a preinstaller package to integrate with the MVC Framework. You can read about it here. If you use NUnit, XUnit, or another testing framework or you wish to build your own, customized testing framework, Joe Cartano has a great walkthrough on creating the project template packages.
For these examples, I’ll use the Microsoft VSTS Testing Framework (built into VS2008, easy, simple, and integrated) and Moq (Scott Hanselman has a great post with some Moq Helpers on it—the Moq helpers will need a bit of tweaking due to casing, but work very well!). I’ve taken the helpers that Scott (and community) have put together and tied them into a MvcMockHelpers library for my own usage. If you’d like the source code and the libraries, you can download them here. The current library includes Moq and RhinoMocks.
I’d like to point out that I’m not an expert with Moq by any stretch of the imagination—I’m sure there are better, faster, more Moq’able ways of doing the Unit Testing… 🙂 Feel free to comment and let me know. 😉 I like the framework and would appreciate any tips and tricks.
Out of the box, we have a nice framework of our MVC web and testing projects. I’ll use good, old reliable Northwind for this example (recent examples have used my Photo Gallery as a data prototype, this is a nice change).
At first glance, the MVC package and solution templates look much the same. Index and About are still prefab’d and based on a single Site.Master page. Routes are still, by default, managed in Global.asax.
So, what’s different at 10,000’? Here’s a brief list and some descriptive information from the gurus.
- Separation of Routing from the MVC Framework itself. Phil Haack explains the separation details here.
- Controller actions no longer require the redundant ControllerAction attribute. Brilliant.
- The ‘MVCToolkit’ that Rob Conery had developed is now integrated into System.Web.Mvc as HtmlHelper.
- Views are generated with code-behind pages now (no longer requiring you to right-click > Convert to Web Application on each that has code-behind coding).
There are a few things that still, in my opinion, should be worked on.
- Extracting the routing from Global.asax as a practice and placing them in an external “RouteManager” class that is either prefab’d from the template. I’m not sure why, but having a few hundred lines in Global.asax bugs me.
- Mocking, as we’ll see in the next posts, still requires faking out some objects, like the Views.
In upcoming posts, I’ll dig into changes in testing, routing, and a bit into using the new HtmlHelpers for generating clean, XHTML compliant code.