Your official information source from the .NET Web Development and Tools group at Microsoft.
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:
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:
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 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.
(Fig 2: Test Framework Dropdown)
You can see that the project contains unimplemented tests and an NUnit.Framework using statement .
(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
Very interesting stuff!
Do you plan to support also mbUnit?
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.
I am attempting to compile the relevant resources for ASP.NET MVC Framework Preview 2 at one place so
Daily link is back after 2 months. Misc Build your own CAB: The Main Players in the Composite Application
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
Fixed it myself. The template should use absolute path instead of relative path.
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.
You may recall ScottGu's post about the new feature in latest drop of the MS MVC framework that allows
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.
Phil has some good thoughts on ASP.NET MVC preview 2 and beyond.   A couple of key things that I
Look at helper for mocking views for testing controllers in MVC at:
Would love to see Moq integrated too :)