Skip to content
September 11, 2011 / pauldundon

Accessing HTTPContext in ASP.NET MVC Test Projects

The Problem: You have a Visual Studio Test Project as part of an MVC solution. Your code relies on HTTPContext (eg, uses the Session or Application objects), but this is not available using the default test context.

The Solution: Several steps are needed:

1. If you are using the ASP.NET development server, change your project properties so that it uses a fixed port number. You must be able to specify a stable URL for your application.

2. Add a file called default.aspx to your MVC solution. This provides an entry point for the test host. If you attempt to use an MVC URL as your entry point, you will receive an error like the following:

The Web request ‘http://localhost:22307/Home/Index’ completed successfully without running the test. This can occur when configuring the Web application for testing fails (an ASP.NET server error occurs when processing the request), or when no ASP.NET page is executed (the URL may point to an HTML page, a Web service, or a directory listing). Running tests in ASP.NET requires the URL to resolve to an ASP.NET page and for the page to execute properly up to the Load event. The response from the request is stored in the file ‘WebRequestResponse_Obfuscate.html’ with the test results; typically this file can be opened with a Web browser to view its contents.

3. Add a test settings file to your solution. Do this by right-clicking on the solution in solution explorer and choosing Add New Item…

4. Double click on the settings file to edit it, and select Hosts. Select ASP.NET as the host type. Enter the full URL of the newly-added default.aspx file as the URL to test and enter the path to the folder holding your MVC project

Other considerations:-

1. You can add an additional test settings file configured to use the default host if you have tests that can run without HTTPContext, and you wish to avoid the overhead of using the ASP.NET host. You can use “Select Active Test Settings” from the Test menu to switch between them.

2. The debugger will not stop at breakpoints in your code under this configuration. You can trigger a break by adding Debug.Assert(False) to your code. When the assertion fails you will see a dialog; selecting “Retry” will prompt you to open a debugger. A similar effect can be attained using Debugger.Break(), but this will cause windows to report that the development server has stopped working.

3. In some circumstances (particularly when breaking in the debugger) the test host will fail to clean up after itself, so that it will refuse to begin subsequent test runs because it detects a previous test run in progress. The error is:

The test adapter (‘Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapter, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’) required to execute this test could not be loaded. Check that the test adapter is installed properly. The ASP.NET Web application at is already configured for testing by another test run. Only one test run at a time can run tests in ASP.NET. If there are no other test runs using this Web application, ensure that the Web.config file does not contain an httpModule named HostAdapter.

In this case, edit the web.config for your MVC solution and remove the two keys beginning “microsoft.visualstudio.teamsystems” under appSettings, as well as the two references to “HostAdapter”.

Advertisements

One Comment

Leave a Comment
  1. Danny Crone (@dannycrone) / Sep 12 2011 10:26 pm

    Nice work Paul

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: