ASP.NET MVC 3 with NUnit Testproject [updated template]

Update [2011-04-03]:Time for another update: The extension is is of today hosted on Visual Studio Gallery. Read ASP.NET MVC NUnit test template on Visual Studio Gallery for further information.

Update [2011-01-22]:
Meanwhile ASP.NET MVC 3 RTM has arrived - this template is still good to go even for RTM version. As you can see in the comments I initially forgotten to explicitly mention that the originally template was x64 only. As an result I tried to change the .bat file accordingly to handle x86 platforms as well. So when you download the newly packed template this should determine if your system is x86 or x64 and call the proper files for installing the template correctly.*
The first deployed template also contained a reference to a specific NUnit version even when _version-specific_ switch for Visual Studio was set to false I now removed this version-reference.*


As most of you may have read in the meanwhile on December, 10. ASP.NET MVC 3 RC 2 was released. (you can read the announcements over @ ScottGu's Blog or you've been haacked)

What ships with that release again is the possibility to create new test-projects (according to a new created projects), from the build-in project templates, which does use the Microsoft unit-testing framework (in the wizard called Visual Studio Unit Test). This isn't anything new!

ASP.NET MVC 3 Project Wizard

Based on Piotr's MVC 2 test-project template I updated it (based on the shipped MvcWebApplicationTestProjectTemplatev3.0.cs.zip) to be compatible with ASP.NET MVC 3 (RC2) and changed the notation to Assert.That(..., Is-constraint) - which I prefer while using NUnit.

So the vstemplate looks like this now:

<vstemplate version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" type="Project">
<templatedata>
<name package="{E0915DB0-C6DC-4BF6-AEBF-9D72308FDB81}">
<description package="{E0915DB0-C6DC-4BF6-AEBF-9D72308FDB81}">
<icon package="{52CBD135-1F97-2580-011F-C7CD052E44DE}" id="531">
<projecttype>CSharp</projecttype>
<projectsubtype>
</projectsubtype>
<sortorder>1000</sortorder>
<createnewfolder>true</createnewfolder>
<defaultname>MvcApplicationTest</defaultname>
<providedefaultname>true</providedefaultname>
<locationfield>Enabled</locationfield>
<promptforsaveoncreation>true</promptforsaveoncreation>
<enablelocationbrowsebutton>true</enablelocationbrowsebutton>
<requiredframeworkversion>4.0</requiredframeworkversion>
<hidden>true</hidden>
<templateid>MvcWebApplicationNUnitTestProjectTemplate.cs.vstemplate.v3</templateid>
</icon></description></name></templatedata>
<templatecontent>
<customparameters>
<customparameter name="$MVCReferences$" value="System.Web.Mvc.dll">
</customparameter></customparameters>
<project file="MvcApplicationTest.csproj" replaceparameters="true">
<projectitem replaceparameters="true" targetfilename="App.config">App.config</projectitem>
<folder name="Controllers" targetfoldername="Controllers">
<projectitem replaceparameters="true" targetfilename="HomeControllerTest.cs">HomeControllerTest.cs</projectitem>
<projectitem replaceparameters="true" targetfilename="AccountControllerTest.cs">AccountControllerTest.cs</projectitem>
</folder>
<folder name="Properties" targetfoldername="Properties">
<projectitem replaceparameters="true" targetfilename="AssemblyInfo.cs">AssemblyInfo.cs</projectitem>
</folder>
</project>
</templatecontent>
</vstemplate>

If you compare it to the old MVC 2 template you will probably notice that the extension wizard is gone. Don't know why - maybe we should ask @haacked about that...

A sample of the generated NUnit-Tests (as of the HomeController):

[TestFixture]
public class HomeControllerTest
{
[Test]
public void Index()
{
// Arrange
HomeController controller = new HomeController();

// Act
ViewResult result = controller.Index() as ViewResult;

// Assert
Assert.That("Welcome to ASP.NET MVC!", Is.EqualTo(result.ViewBag.Message));
}

[Test]
public void About()
{
// Arrange
HomeController controller = new HomeController();

// Act
ViewResult result = controller.About() as ViewResult;

// Assert
Assert.That(result, Is.Not.Null);
}
}

To use the new template you still need a registry key referring to the new package. The one I used is only x64 compatible. Remember: The GUID's of the vstemplate and the registry need to match.

Windows Registry Editor Version 5.00 
[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\MVC3\TestProjectTemplates\NUnit]
"Path"="CSharp\Test"
"Package"="{E0915DB0-C6DC-4BF6-AEBF-9D72308FDB81}"
"Template"="MvcWebApplicationNUnitTestProjectTemplatev3.0.cs.zip"
"TestFrameworkName"="NUnit Test"

Alongside to the updated registry entry I added an install.bat file which does the necessary steps:

  1. insert registry key
  2. copy template archive
  3. update Viusal Studio 2010 project template cache

It isn't much about magic glue in it, just for completeness sake I post the batch-script:

@echo off 
echo ==== Installing ASP.NET MVC 3 NUnit project template ====
echo Updating registry settings [X64]
regedit /s MvcWebApplicationNUnitTestProjectTemplate.reg
echo Copying MvcWebApplicationNUnitTestProjectTemplatev3.0.cs.zip to Visual Studio 2010 project templates folder
xcopy .\MvcWebApplicationNUnitTestProjectTemplatev3.0.cs.zip "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ProjectTemplates\CSharp\Test" /Y /I
echo Updating Visual Studio 2010 project template cache "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe" /setup
echo ==== Done ====
@pause

After executing the install.bat and creating a new ASP.NET MVC 3 application the project template should show up as following:

ASP.NET MVC 3 Project Wizard

I packed an archive containing als the stuff (reg key, vstemplate.zip and install.bat). Feel free to download :-) Happy unit-testing!