ASP.NET MVC Preview 3 Tooling Updates And Updated NUnit Test Project Templates

ASP.NET MVC Preview 3 Tooling Updates And Updated NUnit Test Project Templates

  • Comments 26

Note: Please see this post for an updated set of templates created for ASP.NET MVC RTM: http://blogs.msdn.com/webdevtools/archive/2009/04/28/updated-nunit-templates-for-asp-net-mvc-1-0-rtm.aspx 

 

Earlier this week ScottGu announced the release of ASP.NET MVC Preview 3 and outlined some recent changes to the framework. I wanted to give you an update on the tooling enhancements for preview 3. The two major enhancements you will notice are:

  • Support for Visual Web Developer Express 2008 (SP1)
  • Improved Support for Third Party Templates

Support for Visual Web Developer Express 2008 (SP1)

Visual Web Developer Express 2008 (SP1) now has support for Class Libraries and Web Application Projects. Since MVC Applications are a flavor of Web Application Projects, we are now able to support ASP.NET MVC for Visual Web Developer Express 2008 (SP1). If you have installed Visual Web Developer Express 2008 (SP1), you will notice the ASP.NET MVC templates in the Web Project template section of the New Project Dialog.

New Project Dialog

 

 

 

 

 

 

 

 

 

 

 

 

(Fig 1: Visual Web Developer Express 2008 (SP1) New Project Dialog)

If you are using Visual Web Developer Express 2008 (SP1), the first thing you will probably notice when creating a new ASP.NET MVC Web Application is that no Create Unit Test Project dialog appears. The reason for this is that Visual Studio Team Test is not included with express skus so there are no Visual Studio Test Project templates. You will need to install third party test framework templates to take advantage of the Create Unit Test Project dialog. I outlined the steps for this in a prior blog post but these steps should not be necessary much longer as Phil Haack has been working with MBUnit and XUnit to update their installers for ASP.NET MVC Preview 3 template support.

Improved Support for third party templates

At this point you are probably thinking one of two things:

1. "I don't really care about the improvements, just give me some awesome templates!"

Sure, here you go: NUnit MVC Preview 3 Templates 

Just extract the zip file and run InstallNUnitTemplates.bat or InstallNUnitTemplatesExpress.bat if you have installed VWD Sp1. You will need to run the batch files as an adminstrator if you are running Vista since they run .reg files to register the templates. (x64 is not yet supported in the batch files, coming shortly)

CropperCapture[2]

(Fig 2: Extracted NUnit Template Download)

After running the batch file and starting Visual Studio, you should now see the Create Unit Test Project dialog with the updated NUnit templates. Your project will build out of the box and contain implemented unit tests that will pass in NUnit.

2. "Tell me more about the template improvements, how do I make my own?"

The major improvement we made in the ASP.NET MVC Preview 3 timeframe was to support a new template replacement variable, $mvcprojectname$. You can include this variable anywhere in your test project template and it will be replaced by the name of the parent MVC Project name when a new ASP.NET MVC Application is created and the test project is selected in the Create Unit Test Project dialog. The NUnit Test Project template contains the following two lines in HomeControllerTest.cs:

using $mvcprojectname$;
using $mvcprojectname$.Controllers;

As you can see, when the test project is created, the using statements will refer to the parent MVC Project, allowing the solution to build without having to make any lame code edits.

CropperCapture[7]

(Fig 3: NUnit Test Project With $mvcprojectname$ Parameter Replacement)

(As a side note, now that Actions can return objects of type ActionResult(), unit testing controllers is far easier. The Visual Studio Unit Test templates have also been updated with identical unit tests that are each no more than five lines of code and do not require a mock object framework.)

Lastly, when you choose to create a Visual Studio Unit Test project in the Create Unit Test Project dialog, a custom wizard copies System.Web.Abstractions, System.Web.MVC, and System.Web.Routing to the bin of the test project automatically. If you want to do the same in your third party template, simply modify your .vstemplate file with the bold lines:

<VSTemplate Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">
  <TemplateData>
    <Name>NUnitDemoPreview3</Name>
    <Description>Updated NUnit Test Project</Description>
    <ProjectType>CSharp</ProjectType>
    <ProjectSubType>
    </ProjectSubType>
    <SortOrder>1000</SortOrder>
    <CreateNewFolder>true</CreateNewFolder>
    <DefaultName>NUnitDemoPreview3</DefaultName>
    <ProvideDefaultName>true</ProvideDefaultName>
    <LocationField>Enabled</LocationField>
    <EnableLocationBrowseButton>true</EnableLocationBrowseButton>
    <Icon>__TemplateIcon.ico</Icon>
  </TemplateData>
  <TemplateContent>
    <CustomParameters>
      <CustomParameter Name="$MVCReferences$" Value="System.Web.Abstractions.dll;System.Web.Mvc.dll;System.Web.Routing.dll"/>
    </CustomParameters>

    <Project TargetFileName="NUnitDemoPreview3.csproj" File="NUnitDemoPreview3.csproj" ReplaceParameters="true">
      <Folder Name="Controllers" TargetFolderName="Controllers">
        <ProjectItem ReplaceParameters="true" TargetFileName="HomeControllerTest.cs">HomeControllerTest.cs</ProjectItem>
      </Folder>
      <Folder Name="Properties" TargetFolderName="Properties">
        <ProjectItem ReplaceParameters="true" TargetFileName="AssemblyInfo.cs">AssemblyInfo.cs</ProjectItem>
      </Folder>
    </Project>
  </TemplateContent>
 
<WizardExtension>
    <Assembly>Microsoft.VisualStudio.Web.Extensions, Version=9.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35</Assembly>
    <FullClassName>Microsoft.VisualStudio.Web.Mvc.TestTemplateWizard</FullClassName>
  </WizardExtension>

</VSTemplate>

(Fig 4: .vstemplate file using the MVC Template Wizard)

This addition to your .vstemplate file will rely on TestTemplateWizard to create a custom parameter and add the references from the MVC run-time install location. This is more robust than relying on hint paths to locate the bin references and eliminates difficulty with unresolved references in MVC test projects.

Hope this helps you unit test your ASP.NET MVC Preview 3 apps!

Joe Cartano | SDET | Visual Studio Web Developer

Leave a Comment
  • Please add 3 and 6 and type the answer here:
  • Post
  • Last week I blogged about the ASP.NET MVC Preview 3 release .&#160; One important thing I forgot to mention

  • I am having trouble getting this to work VWD express.  I have a solution set up that has my website and then the NUnitDemoPreview project but I am unable to reference my website from the NUnitDemoPreview project and so none of my controllers are available.  I go to 'Add  Reference', click 'Projects' but my website isn't listed.  Any ideas?  This looks really good and I can't wait to get it up and running.  Thanks for the templates.

  • Ok, just to follow up, I cannot upgrade an old web project but if I create a new one, it works fine.  so, I will just do that and copy my files over.  Thanks for the templates

  • Hi Joel, glad to see you got everything working. If you are trying to test an ASP.Net website as opposed to an ASP.Net Web Application or ASP.Net MVC Application, you should take a look at this link:

    http://msdn.microsoft.com/en-us/library/ms182526.aspx

    It is a bit different because a website is not compiled into one dll so unit testing the website is not as straightforward. If you were having trouble unit testing an existing MVC Web Application, please let me know and we can take a look at what might be going wrong.

    Thanks,

    Joe

  • La inclusión en el SP1 (Beta) de Visual Web Developer Express 2008 de soporte para librerías de clases

  • You are voted (great) - Trackback from Web Development Community

  • Here are links to some blog posts I have previously written: ASP.NET MVC Preview 3 Tooling Updates And

  • 【原文地址】 ASP.NET MVC Preview 3 Release 【原文发表日期】 Tuesday, May 27, 2008 1:26 PM 今天早晨,我们发布了ASP.NET MVC框架的第三个预览版

  • Ist man in Besitz der Standard Edition von Visual Studio 2008, wird man beim Erstellen seines ersten MVC-Projekts erstaunt feststellen, dass da etwas fehlt. Richtig, es wird kein Unit Test Projekt mit erstellt. Der Grund ist “schnell” ausgemacht: […

  • ASP.Net MVC 1.0 RTM has been out for a while and I noticed that people are still downloading an NUnit

  • Here is a cross post from my team's blog: http://blogs.msdn.com/webdevtools ASP.Net MVC 1.0 RTM has been

Page 2 of 2 (26 items) 12