ASP.NET MVC Test Template Project for NUnit and Moq

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

Out of the box, you get the “Visual Studio Unit Test” set which features 25 opinionated Home and Account Controller tests. However, you are 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 Studio 9.0\Common7\IDE\ProjectTemplates\Csharp\Test\1033, execute devenv /setup, wait a while and then update the registry) and have your own test frameworks appear in the “Test Framework” selectable list. It’s just 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.

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

image

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

image

I’ve provided a screenshot of a sample solution after a new ASP.NET MVC 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 project by default.  image

The provided tests are basically a ported from the “Visual Studio Unit Test” template.  There should be no surprises here as all tests should pass from the get go. Please note that I have only included the C# template for the full version of Visual Studio 2008.  I have also included an uninstaller script (Uninstall NUnitWithMoq.bat) so you may install 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.

Comments

  1. Hi,

    Thanks for this template.
    As a user of the “VS 2008 Standard Edition” I need a working template to get the “Create Test” wizard shown.
    I tried the “MVC Preview 3 Template” before but it seems to be outdated.
    The only thing a had to do was to change the line
    copy “.\CSharp\NUnitWithMoq.zip” “%programfiles%\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates\CSharp\Test\1033\”
    to
    copy “.\CSharp\NUnitWithMoq.zip” “%programfiles%\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates\CSharp\Test\”
    in “Install NUnitWithMoq.bat” on my machine.
    Without this the template was not shown on my system.

    I had the same problem with the “MVC Preview 3 Templates” before.

    Nicolai
    German VS 2008 Standard Edition

  2. Thanks for this post, Ben. I’m going to be including a discussion of setup steps on a site I am putting together and I hope it’s all right if I reference this post when it comes to the step of setting up NUnit and MOQ.

    Has anyone using Visual Studio 2008 Professional had the problem of no vsmdi file having been generated from using the template to generate a project? While the test project was set up correctly and the code and references all seem to be intact, there is no vsmdi file, and thus my test view shows no tests, etc. Any ideas on how to fix this, anyone?

    Thanks,

    Mike

  3. @Michael Clagett – By all means, please feel free to reference this post and best of luck on your site. I wish I could offer you help with the vsmdi file. I will see if I can’t find some time to look into this. Thanks for the comment and question.

  4. I have exactly the same problem. I’m under vista and I ran your installation script as admin. I use VS 2008 Pro.
    When I try to run the tests, I get a message saying that the tests are not loaded or are disabled.
    I don’t know how to enable them.

  5. >When I try to run the tests, I get a message saying that the tests are not loaded or are disabled.<
    I am having the same problem :(

    Anyone found out why this happens?

  6. I discovered your weblog site on google and check just a few of your early posts. Continue to maintain up the very good operate. I simply further up your RSS feed to my MSN Information Reader. Seeking forward to studying more from you afterward!…

  7. Thanks for the marvelous posting! I certainly enjoyed reading it, you happen to be a great author.I will be sure to bookmark your blog and will come back very soon. I want to encourage one to continue your great job, have a nice weekend!

closed