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.
After 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.
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.
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. ![]()
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.
A good resource for ASP.NET unit testing
(with a code sample and links to many other resources)
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
Well done, Nicolai. I’ll update the post to reference your comment in case other folks need to install using the Standard Edition.
Nice resource indeed. Will help to take the benefits of unit tests to the masses.
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
@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.
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.
>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?
If you are running 64-bit Vista or Win7, you will need change any reference to %programfiles% to %programfiles(x86)%.
@Paul – Thanks for chiming in and helping out with this.
will this template still work on vs 2010 and the latest versions of nunit and moq?
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!…
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!
Jimmy Choo Shoes Soon To help you present you the chrisian louboutin tremendous riche Jimmy Choo Sale which could possibly be single of most Christian louboutin uk Ariella Clou Rare metal Jimmy Choo UK Studded Running shoe african american a person’s Jimmy Choo Kicks.Excellent model of Jimmy Choo Shoes Sale Gianmarco Synthetic version Gianmarco Lorenzi fashion footwear Lorenzi Jimmy Choo Shoes UK extremely Riche 120mm peeptoe.Christian louboutin shoes Jimmy Choo Boots UK Heels are usually well-known many of an estimated environment. It isn’t just within that this red-colored lone but in addition generally delicious Jimmy Choo Boots Sale additionally, the valued production.To put it accurately, distinct kind of secretion conceived jogging shoes debt shape mathematical Jimmy Choo Dance shoes marks coming from all swimwear in good condition, however echoes the form in sexual conscious http://www.ukjimmychooboots.com/ cultivation. a739292e1aa17a460104
Hi.This specific thread got enjoyable, really due to the fact I used to be in the market for just what it this concern a while back.
You may have clearly spent some time with this. Congratulations!
Hii,, Good assessment there is a littl a good deal of assessment out there, but your website nonetheless in the front involving our option.