Sunday, 3 May 2009

Updated templates for xUnit.net in ASP.NET MVC RTM

Environment:

  • Visual Studio 2008 Pro
  • xUnit.net v1.5 (build 1.5.0.1479)

xUnit.net has very few tests by default, so I have adapted the standard Visual Studio tests generated for ASP.NET MVC web applications. This resulted in two files: AccountControllerTests.cs and HomeControllerTests.cs (the last one is similar with HomeControllerFacts.cs)

The xUnit.net template for ASP.NET MVC unit testing can be found on the following location:

%VS2008%Common7IDEProjectTemplatesCSharpTestXunitMvcTestProjectTemplate.cs.zip

After unpacking the content of the template to a temp folder, we must place the new files in the Controllers folder, and update the content of MvcTemplate.csproj and MyTemplate.vstemplate:

MvcTemplate.csproj:

...
<ItemGroup>
  <Compile Include="ControllersHomeControllerFacts.cs" />
  <Compile Include="ControllersHomeControllerTests.cs" />
  <Compile Include="ControllersAccountControllerTests.cs" />
...
MyTemplate.vstemplate:

...
<Project TargetFileName="MvcTemplate.csproj" File="MvcTemplate.csproj" ReplaceParameters="true">
  <Folder Name="3rdParty" TargetFolderName="3rdParty">
    <ProjectItem ReplaceParameters="false" TargetFileName="xunit.dll">xunit.dll</ProjectItem>
  </Folder>
  <Folder Name="Controllers" TargetFolderName="Controllers">
    <ProjectItem ReplaceParameters="true" TargetFileName="HomeControllerFacts.cs">HomeControllerFacts.cs</ProjectItem>
    <ProjectItem ReplaceParameters="true" TargetFileName="HomeControllerTests.cs">HomeControllerTests.cs</ProjectItem>
    <ProjectItem ReplaceParameters="true" TargetFileName="AccountControllerTests.cs">AccountControllerTests.cs</ProjectItem>
  </Folder>
...

After re-packing the template and replacing the original one, the only thing left to do is running devenv /setup in the %VS2008%Common7IDE folder in order to update the template cache of Visual Studio.

The updated template can be downloaded from here - please rename the file to XunitMvcTestProjectTemplate.cs.zip