ASP.NET MVC Preview 3 Tooling Updates And Updated NUnit Test Project Templates
Note: Please see this post for an updated set of templates created for ASP.NET MVC RTM: http://blogs.msdn.com/webdevtools/archive/2009/04/28/updated-nunit-templates-for-asp-net-mvc-1-0-rtm.aspx
Earlier this week ScottGu announced the release of ASP.NET MVC Preview 3 and outlined some recent changes to the framework. I wanted to give you an update on the tooling enhancements for preview 3. The two major enhancements you will notice are:
- Support for Visual Web Developer Express 2008 (SP1)
- Improved Support for Third Party Templates
Support for Visual Web Developer Express 2008 (SP1)
Visual Web Developer Express 2008 (SP1) now has support for Class Libraries and Web Application Projects. Since MVC Applications are a flavor of Web Application Projects, we are now able to support ASP.NET MVC for Visual Web Developer Express 2008 (SP1). If you have installed Visual Web Developer Express 2008 (SP1), you will notice the ASP.NET MVC templates in the Web Project template section of the New Project Dialog.
(Fig 1: Visual Web Developer Express 2008 (SP1) New Project Dialog)
If you are using Visual Web Developer Express 2008 (SP1), the first thing you will probably notice when creating a new ASP.NET MVC Web Application is that no Create Unit Test Project dialog appears. The reason for this is that Visual Studio Team Test is not included with express skus so there are no Visual Studio Test Project templates. You will need to install third party test framework templates to take advantage of the Create Unit Test Project dialog. I outlined the steps for this in a prior blog post but these steps should not be necessary much longer as Phil Haack has been working with MBUnit and XUnit to update their installers for ASP.NET MVC Preview 3 template support.
Improved Support for third party templates
At this point you are probably thinking one of two things:
1. “I don’t really care about the improvements, just give me some awesome templates!”
Sure, here you go: NUnit MVC Preview 3 Templates
Just extract the zip file and run InstallNUnitTemplates.bat or InstallNUnitTemplatesExpress.bat if you have installed VWD Sp1. You will need to run the batch files as an adminstrator if you are running Vista since they run .reg files to register the templates. (x64 is not yet supported in the batch files, coming shortly)
(Fig 2: Extracted NUnit Template Download)
After running the batch file and starting Visual Studio, you should now see the Create Unit Test Project dialog with the updated NUnit templates. Your project will build out of the box and contain implemented unit tests that will pass in NUnit.
2. “Tell me more about the template improvements, how do I make my own?”
The major improvement we made in the ASP.NET MVC Preview 3 timeframe was to support a new template replacement variable, $mvcprojectname$. You can include this variable anywhere in your test project template and it will be replaced by the name of the parent MVC Project name when a new ASP.NET MVC Application is created and the test project is selected in the Create Unit Test Project dialog. The NUnit Test Project template contains the following two lines in HomeControllerTest.cs:
As you can see, when the test project is created, the using statements will refer to the parent MVC Project, allowing the solution to build without having to make any lame code edits.
(Fig 3: NUnit Test Project With $mvcprojectname$ Parameter Replacement)
(As a side note, now that Actions can return objects of type ActionResult(), unit testing controllers is far easier. The Visual Studio Unit Test templates have also been updated with identical unit tests that are each no more than five lines of code and do not require a mock object framework.)
Lastly, when you choose to create a Visual Studio Unit Test project in the Create Unit Test Project dialog, a custom wizard copies System.Web.Abstractions, System.Web.MVC, and System.Web.Routing to the bin of the test project automatically. If you want to do the same in your third party template, simply modify your .vstemplate file with the bold lines:
<VSTemplate Version=”2.0.0″ xmlns=”http://schemas.microsoft.com/developer/vstemplate/2005″ Type=”Project”>
<Description>Updated NUnit Test Project</Description>
<CustomParameter Name=”$MVCReferences$” Value=”System.Web.Abstractions.dll;System.Web.Mvc.dll;System.Web.Routing.dll”/>
<Project TargetFileName=”NUnitDemoPreview3.csproj” File=”NUnitDemoPreview3.csproj” ReplaceParameters=”true”>
<Folder Name=”Controllers” TargetFolderName=”Controllers”>
<ProjectItem ReplaceParameters=”true” TargetFileName=”HomeControllerTest.cs”>HomeControllerTest.cs</ProjectItem>
<Folder Name=”Properties” TargetFolderName=”Properties”>
<ProjectItem ReplaceParameters=”true” TargetFileName=”AssemblyInfo.cs”>AssemblyInfo.cs</ProjectItem>
<Assembly>Microsoft.VisualStudio.Web.Extensions, Version=22.214.171.124, Culture=neutral, PublicKeyToken=31bf3856ad364e35</Assembly>
(Fig 4: .vstemplate file using the MVC Template Wizard)
This addition to your .vstemplate file will rely on TestTemplateWizard to create a custom parameter and add the references from the MVC run-time install location. This is more robust than relying on hint paths to locate the bin references and eliminates difficulty with unresolved references in MVC test projects.
Hope this helps you unit test your ASP.NET MVC Preview 3 apps!
Joe Cartano | SDET | Visual Studio Web Developer