ASP MVC 2 is available as a separate download for Visual Studio 2008 and is by default installed in Visual Studio 2010. If you create new ASP MVC 2 Web Application project, this is the view you get in VS 2010:
You just need to pick ASP .NET MVC 2 under a list of available templates, provide a name for your project (or stick with default, but this is definitely not recommended for any production code – you wouldn’t like to have your projects named ASP MVC app2, app3, etc….) and click OK. This is exactly the same as for any other kind of project templates available in Visual Studio. But for ASP MVC 2 there is a additional step involved:
You can choose for Visual Studio to create a unit test project automatically for you. After it’s created it will contain unit tests for the controllers used in MVC project (17 unit tests all together). Out of the box Visual Studio supports adding unit test project for Visual Studio Unit Test framework only. So I decided to extend it to support NUnit based unit test projects as well. What I’ve done besides was to change unit test names as so their naming would be compliant with the one we’re using at my office and also, at least in my opinion is a little bit more readable. So as an example:
Additionally there was a method used in Microsoft’s unit tests, which is obsolete in NUnit so this one also got updated. Instead of:
we now have much cleaner, more readable, elegant, generic method:
It wasn’t the most challenging task I’ve ever had to deal with, but it wasn’t the easiest thing to do either so I decided I’ll give you a sample files and a step by step instruction on what’s the easiest way to do it. This actually consists of three steps only so here you go:
- Download NUnitTest.zip file and copy it to the templates folder, which is:
“c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ProjectTemplates\CSharp\Test\”
(Program Files (x86) is present on x64 systems, on 32 bit version of Window, this will be Program Files directory)
Download and run nunit.reg which contains some of the keys and values that need to be entered in your registry, to allow integration with Visual Studio (NUnit based unit tests project becomes one of the options on the dropdown menu for test framework). This is also for x64 systems, edit .reg file (e.g. in Notepad) to modify it for 32bit systems.
Run devenv.exe /setup to have it registered in your Visual Studio
That’s it! Now, when you open up Visual Studio and choose MVC 2 Web Application project “Test framework” dropdown will contain NUnitFramework as another option:
Choosing it will create test project with a reference to NUnit.dll, unit tests with changed naming, methods updated to current NUnit API and reference to a Web Project that is created. Everything builds out-of-the box (you just need to fix path to Lib folder where NUnit libraries are stored on your machine), all unit tests pass and solution does not contain any Code Analysis warnings. So there you go, enjoy your coding!