ASP.NET MVC Test Framework Integration Walkthrough

ASP.NET MVC Test Framework Integration Walkthrough

Rate This
  • Comments 28

Note: Please see this post for an updated set of templates created for ASP.NET MVC RTM:  

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 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.    


    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:


    (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 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:


    (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!


    (Fig 5: Tests Passing in the NUNit GUI)

    Hope this helps you unit test your mvc apps!

    Joe Cartano | SDET | Visual Studio Web Developer

    Leave a Comment
    • Please add 5 and 3 and type the answer here:
    • Post
    • Hi Dcazzulino,

          With the support available within VS it would be simple to use any other mock library too... If you get an opportunity you can very easily do so using the steps described in the post

         Hope this helps...


    • I'm slowly recovering from keynoting at MIX last week, and have been digging my way out of backlogged

    • 摘要本期共有7篇文章(实际上有12篇文章,这两天不知为何网络状况一直不好,很多国外网站都上不去,只好暂时作罢):深入ASP.NETAJAX:对象继承体系使用ASP.NET3.5和自定义层...

    • I followed the instructions to get nUnit templates running but I still do not see it as an option in the drop down.  All the registry entries exist, the is in C:\program files\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates\CSharp\Test\1033, and I ran devenv without visual studio open.  Any ideas?

    • Hi lcj,

         Could send a snapshot of your registry, the snapshot of the way your dropdown currently looks and you test project to I will try to take a look...


      Vishal R. Joshi

    • 摘要本期共有7篇文章(实际上有12篇文章,这两天不知为何网络状况一直不好,很多国外网站都上不去,只好暂时作罢):深入ASP.NETAJAX:对象继承体系使用ASP.NET3.5和自定义层级数...

    • Hi lcj,

      If you go to file -> new project -> Visual C# -> Test do you see the template there? If not, it might be worth it to try this step again: " navigate to  "%Program Files%\Microsoft Visual Studio 9.0\Common7\IDE and type devenv /setup". If you do see the template there, then something in the registry is probably incorrect. This is just a general guideline, I will take a look with Vishal at what you send him to narrow down why you can't see the additional template.



    • Earlier this week, I wrote about the latest release of RC2. Since that time, Brad Wilson and

    • Earlier this week ScottGu announced the release of ASP.NET MVC Preview 3 and outlined some recent changes

    • First of all I did what as every developer experienced in the RTFM methodology should do I went to it's

    • Here are links to some blog posts I have previously written: ASP.NET MVC Preview 3 Tooling Updates And

    • The ASP.NET MVC 1.0 Release Candidate (RC) is finally out, and we wanted to give returning MVC users

    • So you have downloaded ASP.NET MVC. Chosen File -> New project and setup your new Asp.NET MVC project

    Page 2 of 2 (28 items) 12