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 6 and 4 and type the answer here:
  • Post
  • PingBack from http://www.basketballs-sports.info/basketball-chat/?p=1540

  • Good write-up.  Its very exciting to see MVC working on VWD.  But, there's a problem with the InstallNUnitTemplatesExpress.bat script.  The unit test dialog box wouldn't work in VWD 2008 SP1 until I manually copied the 2 template zip files into the proper language folders in C:\Documents and Settings\{UserName Goes Here}\My Documents\Visual Studio 2008\Templates\ProjectTemplates.  Looks like the folder structure specified in InstallNUnitTemplatesExpress.bat doesn't exist.

  • Sorry, I didn't clearly state that I'm using the Express version of VWD.

  • Last week I blogged about the ASP.NET MVC Preview 3 release .&#160; One important thing I forgot to mention

  • Last week I blogged about the ASP.NET MVC Preview 3 release .&#160; One important thing I forgot to mention

  • Last week I blogged about the ASP.NET MVC Preview 3 release .&#160; One important thing I forgot to mention

  • 【原文地址】 ASP.NET MVC Support with Visual Web Developer 2008 Express 【原文发表日期】 Sunday, June 01, 2008 6:50

  • Last week I blogged about the ASP.NET MVC Preview 3 release .&#160; One important thing I forgot to mention

  • Vorige week blogde ik over de ASP.NET MVC Preview 3 release . Eén belangrijk ding ben ik vergeten mee

  • What's the estimated time of release for sp1?

  • My latest in a series of the weekly, or more often, summary of interesting links I come across related to Visual Studio. Greg Duncan posted a link to a free e-book: TCP/IP Fundamentals for Microsoft Windows . Ayende announced that Visual Studio integration

  • Недавно аннонсированый ASP.NET MVC Preview 3 одинаково работает с Visual Studio 2008 и Visual Web Developer

  • "Good write-up.  Its very exciting to see MVC working on VWD.  But, there's a problem with the InstallNUnitTemplatesExpress.bat script.  The unit test dialog box wouldn't work in VWD 2008 SP1 until I manually copied the 2 template zip files into the proper language folders in C:\Documents and Settings\{UserName Goes Here}\My Documents\Visual Studio 2008\Templates\ProjectTemplates.  Looks like the folder structure specified in InstallNUnitTemplatesExpress.bat doesn't exist."

    Hi Matthew,

    Thanks for the catch. I had forgotten to create the directory for the express templates first as there is no "test" category on the express sku. I just tried out the updated version and all looks well. Also, I should mention that the batch file doesn't yet work for x64 machines, I will work on uploading a batch file to support x64.

    Joe

  • Na última semana eu escrevi sobre o lançamento da ASP.NET MVC Preview 3 . Uma coisa importante que esqueci

  • Je viens juste de publier un poste sur la Preview 3 du framework ASP.NET MVC et j'ai oubli&#233; de signaler

Page 1 of 2 (26 items) 12