Thursday, January 13, 2011

Unit testing fun under Visual Studio 2010 Ulitmate

Recently I encountered a few interesting problem while unit testing an assembly in VS 2010. The assembly I was testing referenced web based code, HttpContext.Current, and was designed for use within a web application.

Unit tests don't run within a web context so testing assemblies like this is obviously problematic. One solution to this problem is to add the following to the head of your test methods:
[TestMethod()]
[HostType("ASP.NET")] 
[UrlToTest("http://localhost/SomeWebApp/Default.aspx")]
public void SomeTestMethod()
{
    ...
These directives instruct Visual Studio to run your unit test inside an  ASP.NET Host.  When the test is run the web app referenced by UrlToTest is fired up and the tests are run within this web site just after the page load event of the referenced web page.

This solution works fine in VS 2008 but when i try this in VS 2010 I get the following error:

The test adapter 'WebHostAdapter' threw an exception while running test 'testWebContext'. The web site could not be configured correctly; getting ASP.NET process information failed. Requesting 'http://localhost/someApp/VSEnterpriseHelper.axd' returned an error: The remote server returned an error: (500) Internal Server Error.
The remote server returned an error: (500) Internal Server Error.


I found the following "solutions" online:
  1. Set the test project and the web project to use version 4.0 of the .Net framework

  2. Add the following to your the Web.config file of your web site:
      <location path="VSEnterpriseHelper.axd">
        <system.web>
          <authorization>
            <allow users="*"/>
          </authorization>
        </system.web>
      </location>
  3. Add the following to your the Web.config file of your web site:
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"/>
    
  4. Give the Network Service group permissions to everything under the web root of your web site / application. I gave full permissions to the Internet guest account (pcname\IUSR_pcname), the IIS process account (pcname\IWAM_pcname), the Network Service account.
None of these things worked for me or at least not under IIS. The only thing that worked in the end, after one very frustrating day, was to set my web site to .Net 4.0 and to use the development server that comes bundled with Visual Studio as the host (the one that is used for "File System" web sites).

To use the Development Server you need to add one more directive to your test method:
[AspNetDevelopmentServerHost("D:\\SomeWebApp\""/SomeWebApp")]
Your Method should now look something like this:
[TestMethod()]
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("D:\\SomeWebApp\", "/SomeWebApp")]
[UrlToTest("http://localhost:2100/Default.aspx")]
public void SomeTestMethod()
{
     ...
Using the Development server  is not an ideal solution for me, nor is only being able to unit test version 4.0 apps, but this seems to be the only way it will work.

I hope this saves someone else having to bang their head against the wall for a day.

cheers

No comments:

Post a Comment