ASP.NET MVC Test Framework Integration Walkthrough

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  


Vishal’s earlier blog post, ASP.NET MVC Test Framework Integration, outlined the motivation and steps for registering a third party test framework for use with ASP.NET MVC Applications. 


 “In lines with best practices one of the goals of this new ASP.NET framework is to enable easy unit testable web applications.  To ease unit testing and to make it part of our regular application development workflow Visual Studio is introducing integration of unit test projects with ASP.NET MVC Application…”


 Today, I will work through the steps Vishal outlined with some actual templates I have created and then run the tests using the corresponding third party framework. Below are the relevant steps Vishal outlined in ASP.NET MVC Test Framework Integration:


  • Remove the template.zip from the user specific directory and move it to %Program Files%\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates\Language\Test\Locale
  • Close all the instances of Visual Studio and open the command prompt (Start –> Run type cmd)
  • Navigate to the IDE folder on the command prompt (cd %Program Files%\Microsoft Visual Studio 9.0\Common7\IDE) and type devenv /setup, hit enter
  • This will take some time but should setup your newly created project template within Visual Studio .  If you now open VS 08, then in New Project dialog box you should be able to see your template show up under C#/VB–>Test directory.  

  • Lets work through these steps to create a new ASP.Net MVC Application, select a third party test framework to test the project with, and actually run the tests included in the third party test project template.


    The template zip files and their accompanying .reg files can be downloaded here: MVC Demo Files


    About the templates:


    NUnit Demo (C#/VB): This template is a similar in look to the Visual Studio Unit Test project but it relies on NUnit.


    NUnit And Rhino Mocks (C#)*: This template is actually more of a starter kit or demo with complete tests for default routing and the index() action. Most of the code, most notably the MvcMockHelpers class, was created by Phil Haack on the ASP.Net team.


     * You will notice that there is a version of the template that includes a template wizard and one that does not. The included wizard will save you the trouble of replacing $mvcprojectname$ with the actual parent project name in two using statements in the test project template. If you want to use the version with the wizard, gac mvcWizard.dll or add it to %Program Files%\Microsoft Visual Studio 9.0\Common7\IDE\. If you don’t mind making the two simple substitutions yourself, go ahead and use the version of the template that does not include the wizard.  For the RTM release, we are looking to automate the replacement of $mvcprojectname$ without having to rely on a template wizard.


    Prerequisites: You will need to install NUnit for both templates and Rhino Mocks if you wish to use the second template.    



    Steps


    1.    Copy the template zip files to %Program Files%\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates\CSharp\Test\1033


    2.    Run the following reg files contained in MVC Demo Files:



    • NUnitDemo.reg

    • NUnitAndRhinoMocks.reg

    Here are the contents of the registry after the reg files have been run:


    CropperCapture[1]


    (Fig 1: Registry Contents After Running .reg Files)


    ASP.NET MVC Test Framework Integration explains the purpose of each of these registry values. You can see that the path corresponds to the location that the templates were copied to in step 1. The display name that will be visible in the test framework dropdown corresponding to NUnitDemo.zip is “NUnit Demo”.


    3.    Close all the instances of Visual Studio and open a Visual Studio command prompt


    4.    Navigate to %Program Files%\Microsoft Visual Studio 9.0\Common7\IDE and type devenv /setup


    The next time you open Visual Studio and create a new C# ASP.NET MVC Web Application, the Create Test Project dialog dropdown will be populated with the two new third party frameworks. Select Nunit Demo.


    Create Test Project Dropdown


    (Fig 2: Test Framework Dropdown)


    You can see that the project contains unimplemented tests and an NUnit.Framework using statement .


    Unit Test Demo Project


    (Fig 3: Unimplemented NUnit tests in the NUnit Demo Template)


    Lets run the test in Nunit.


    5.    Build the project in Visual Studio


    6.    Open the test project dll in Nunit. You should see something like this:


    NUnit


    (Fig 4: Test Project Opened in the NUnit GUI)


    Notice that the About and Index tests appear in the NUnit GUI


    7.    Run the test project and all of your tests should pass. Don’t get too excited though, you haven’t implemented them yet :)


    You can repeat steps 5-7 after creating a new C# ASP.NET MVC Web Application and selecting the Nunit And Rhino Mocks dropdown. If you are using the version of the Nunit And Rhino Mocks template without the wizard, simply replace $mvcprojectname$ with the actual project name of the ASP.NET MVC Application parent project, and your project should build with no errors. Here is a screen shot of implemented routing tests and of all of the tests passing in Nunit. Pretty cool!


    NUnitRhino


    (Fig 5: Tests Passing in the NUNit GUI)


    Hope this helps you unit test your mvc apps!


    Joe Cartano | SDET | Visual Studio Web Developer