高华健是哪里人:ASP.NET MVC Test Template Project for NUnit and Moq | JohnnyCoder

来源:百度文库 编辑:九乡新闻网 时间:2024/10/02 17:50:45

ASP.NET MVC Test Template Project for NUnit and Moq

If you have been following along with ASP.NET MVC, youknow testability is a big part of the story. This fact is really drivenhome when the option to create a test project immediately follows thatof a ASP.NET MVC Web Application. This is all really cool – especiallythe flexible test project templates.

Out of the box, you get the “Visual Studio Unit Test” set whichfeatures 25 opinionated Home and Account Controller tests. However, youare not limited to just this template. Nope. You can build your own test project templates (New Project –> Add References, etc –> File –> Export Template), jump through a few hoops (update the vstemplate file settings,copy the exported files to %Program Files%\Microsoft Visual Studio9.0\Common7\IDE\ProjectTemplates\Csharp\Test\1033, execute devenv/setup, wait a while and then update the registry) and have your owntest frameworks appear in the “Test Framework” selectable list. It’sjust that easy.

My team is currently using NUnit and Moq so this afternoon I exported a custom template project and put together an install script which I based somewhat on the NUnit MVC Preview 3 Templates.  After a few attempts, it all came together.

Afterrunning the .bat installer (Install NUnitWithMoq.bat), you will findthe “NUnit with Moq” template listed in the “Test Framework” dropdown(see above screenshot .) The new template will also appear under theTest project type templates.

The template includes references to the moq.dll andnunit.framework.dll.  Since there’s no way of knowing where each ofthese files will sit on your machine, the templates make the assumptionthat they will be inside an “External Dependencies” folder which livesside-by-side with the test project.  I suggest putting your “ExternalDependencies” folder/files in place before you create your MVC and Testproject.  You shouldn’t have any troubles if you follow this pattern. If not, you’ll merely need to reset the Moq and NUnit references onceyour test project is created. No big deal.

I’ve provided a screenshot of a sample solution after a new ASP.NETMVC Web Application (MvcApplication1) and NUnit with Moq Test Project(MvcApplication1.Test) are created.  As you see, the web project, Moq,NUnit and a handful of other references are included in the test projectby default. 

The provided tests are basically a ported from the “Visual StudioUnit Test” template.  There should be no surprises here as all testsshould pass from the get go. Please note that I have only included theC# template for the full version of Visual Studio 2008.  I have alsoincluded an uninstaller script (Uninstall NUnitWithMoq.bat) so you mayinstall with little worry.

Download NUnit and Moq ASP.NET MVC Test Template: NUnitWithMoq.zip

Update 04/03/2009: If you are running VS 2008 Standard Edition, refer to Nicolai Stoy’s comment which calls out how to update the install .bat file.