Welcome to MSDN Blogs Sign in | Join | Help

Visual Web Developer Team Blog

Your official information source from the Visual Web Developer team.

News

  • These postings are provided "AS IS" with no warranties, and confer no rights. Use of included code samples are subject to the terms specified Terms of Use
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

    Posted: Thursday, March 06, 2008 2:50 PM by WebDevTools

    Comments

    Claudio Maccari said:

    Very interesting stuff!

    Do you plan to support also mbUnit?

    Many thks

    makka

    http://www.ugialt.net

    # March 7, 2008 9:04 AM

    Jason Haley said:

    # March 7, 2008 10:14 AM

    WebDevTools said:

    Hi Claudio, we have been working with MbUnit, NUnit, and more to incorporate this process into their installation. The goal is to simply download the latest version of a third party framework and then see their templates appear in the dropdown. You can see the first attempt at this if you download one of the following versions of MbUnit:

    MbUnit v2: http://mb-unit.googlecode.com/files/MbUnit%20v2.4.2%20Preview.exe

    MbUnit v3: http://mb-unit.googlecode.com/files/Gallio%20v3.0%20Alpha%202%20Preview%20-%20Bundle%20with%20MbUnit%20v3.exe

    There are still some minor issues to work through with the sample templates but it should give you an idea of what the end result will look like. This is probably the coolest part of the feature and we will blog about it once we make more progress.

    # March 7, 2008 1:15 PM

    Your Websites, Our Passion! said:

    I am attempting to compile the relevant resources for ASP.NET MVC Framework Preview 2 at one place so

    # March 7, 2008 1:19 PM

    Trumpi's blog said:

    Daily link is back after 2 months. Misc Build your own CAB: The Main Players in the Composite Application

    # March 9, 2008 9:27 AM

    t800t8 said:

    When I tried to use these templates, the test project cannot resolve references for System.Web.Mvc, System.Web.Routing and System.Web.Abstractions while web project can resolve it.

    Please check it. Thanks

    # March 10, 2008 8:41 AM

    t800t8 said:

    Fixed it myself. The template should use absolute path instead of relative path.

    # March 10, 2008 9:31 AM

    WebDevTools said:

    t800t8,

    Thanks for the catch. Originally the projects were not created in Visual Studio 2008/Projects. I changed the hint paths in the csproj files so that they should now resolve correctly when creating a new project in this directory. I left them as relative paths because this is the default behavior in Visual Studio, but let me think about whether it might make more sense to use absolute paths in the templates.

    Thanks,

    Joe

    # March 10, 2008 1:26 PM

    Andrew Stopford's Weblog said:

    You may recall ScottGu's post about the new feature in latest drop of the MS MVC framework that allows

    # March 10, 2008 5:49 PM

    t800t8 said:

    Joe,

    When I use the previous version of templates (before you updated to fix the path to assemblies as I figured out), I found that in web project, the CSPROJ file uses absolute paths, but not relative paths.

    Regards,

    t800t8

    # March 10, 2008 6:28 PM

    Brad Abrams said:

    Phil has some good thoughts on ASP.NET MVC preview 2 and beyond.   A couple of key things that I

    # March 10, 2008 10:35 PM

    Noticias externas said:

    Phil has some good thoughts on ASP.NET MVC preview 2 and beyond.   A couple of key things that I

    # March 10, 2008 11:34 PM

    Programming said:

    Phil has some good thoughts on ASP.NET MVC preview 2 and beyond.   A couple of key things that I

    # March 11, 2008 1:20 AM

    Gil Zilberfeld said:

    Look at helper for mocking views for testing controllers in MVC at:

    http://blog.typemock.com/2008/03/testing-mvc-controllers-in-3-lines-of.html

    # March 11, 2008 3:13 AM

    dcazzulino said:

    Would love to see Moq integrated too :)

    (http://code.google.com/p/moq/)

    # March 11, 2008 10:19 PM

    WebDevTools said:

    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 http://blogs.msdn.com/webdevtools/archive/2008/02/18/asp-net-mvc-test-framework-integration.aspx

       Hope this helps...

    -Vishal

    # March 11, 2008 11:18 PM

    ASPInsiders said:

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

    # March 14, 2008 3:21 AM

    Dflying Chen said:

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

    # March 16, 2008 9:42 AM

    lcj said:

    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 MVCDemo.zip 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?

    # March 16, 2008 2:52 PM

    WebDevTools said:

    Hi lcj,

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

    wishes

    Vishal R. Joshi

    # March 16, 2008 5:00 PM

    Jacky_Xu said:

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

    # March 16, 2008 9:52 PM

    WebDevTools said:

    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.

    Thanks,

    Joe

    # March 18, 2008 12:09 PM

    Matthew Podwysocki's Blog said:

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

    # April 4, 2008 6:36 PM

    Your Websites, Our Passion! said:

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

    # May 30, 2008 9:00 PM

    DeusExMachina said:

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

    # June 28, 2008 1:26 AM

    MVC Diaries said:

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

    # December 4, 2008 2:23 PM

    Visual Web Developer Team Blog said:

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

    # January 27, 2009 4:12 PM

    Eamonn's Blog said:

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

    # April 4, 2009 12:24 PM
    New Comments to this post are disabled
    Page view tracker