Returning to .NET has been exciting after a long time away playing in Java and Ruby land. I can't say I'll miss XML config files and all that junk.
First thing I did after creating my first ASP.NET MVC project was to add a template to easily create a project to test it. The new template will create a class library project with MVC, nunit and my mocking framework of choice Rhino included and ready to go.
When you use VS.NET 2008 to create an MVC web application, it asks if you want to add a testing project to go alongside. Unfortunately, the only choice of test project template you get is the Microsoft one and I like nunit much much more.
This is how we do it...
Set the project up the way you want it to appear new
First thing I did after creating my first ASP.NET MVC project was to add a template to easily create a project to test it. The new template will create a class library project with MVC, nunit and my mocking framework of choice Rhino included and ready to go.
When you use VS.NET 2008 to create an MVC web application, it asks if you want to add a testing project to go alongside. Unfortunately, the only choice of test project template you get is the Microsoft one and I like nunit much much more.
This is how we do it...
Set the project up the way you want it to appear new
- Create a new C# class library project. Give it a general name which will apply to your test project template.
- Add a reference to the System.Web.MVC dll.
- Add a reference to the nunit.framework dll.
- Add a reference to the Rhino.Mocks dll.
- Set the Copy Local property for all of the above dll references to True.
- Create a Controller folder under the project root.
- Add a class HomeControllerTest to the Controller folder.
- Add unit tests to cover the About and Index actions.
- Build your project to get the binaries.
Export the project template
- Go to File --> Export Template and use the wizard to export the template. Call it NUnitTest. You will be told where the zip file is saved to.
- Be sure to chose the option to include the icon.
- Move the NUnitTest.zip to %Program Files%\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates\Csharp\Test\1033. This might be different for you.
- Quit Visual Studio.
- Open a command prompt and navigate to %Program Files%\Microsoft Visual Studio 9.0\Common7\IDE.
- Execute devenv /setup and go make a cup of coffee.
- If you now open VS.NET 2008 and create a new project, you should be able to see your template show up under the C#-->Test directory.
- Add the registry key below by copying the text in to a file and calling it nunit.reg. Run this regfile to add the key. You can add it manually also using regedit.
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\MVC\TestProjectTemplates\NUnit\C#]
"Template"="NUnitTest.zip"
"Path"="CSharp\\Test"
"TestFrameworkName"="nunit Test"
"AdditionalInfo"="http://msdn2.microsoft.com/en-us/library/ms243147(VS.80).aspx"
"Package"=""
1 comment:
Learned something new, thanks
Post a Comment