ASP.NET unit testing Windows7/IIS7

2020-04-07 03:03发布

问题:

Spent several hours today trying to write some unit tests against an ASP.NET project. It's Visual Studio 2010.

Using Windows 7 Enterprise with IIS7.

Steps I took were:

  1. Added a new test project to the solution
  2. Opened a class file as part of the web site (Member.vb)
  3. Right clicked within the class file and "Generate unit tests"
  4. Select the methods I wish to generate stubs for, choose to add to my test project, click OK
  5. Open up the generated MemberTest.vb file in the test project, click within one of the generated tests, click "Run tests in curent context"

When following these precise steps on my Windows XP Professional with IIS6 machine it works fine.

However on the Windows 7 Enterprise machine on IIS7 I get:

The URL specified ('http://localhost/MyProject') does not correspond to a valid directory. Tests configured to run in ASP.NET in IIS require a valid directory to exist for the URL. The URL may be invalid or may not point to a valid Web application.

So what's going on, I can confirm I can browse to http://localhost/MyProject and it displays perfectly.

I feel sure I'm missing some sort of config in Windows/IIS but I'm really at a loss.

Generated test method:

<TestMethod(), _
 HostType("ASP.NET"), _
 UrlToTest("http://localhost/MyProject")> _
Public Sub MyMethodTest()
    Dim target As Member_Accessor = New Member_Accessor() ' TODO: Initialize to an appropriate value
    Dim CurrentVal As Short = 0 ' TODO: Initialize to an appropriate value
    Dim expected As Short = 0 ' TODO: Initialize to an appropriate value
    Dim actual As Short
    actual = target.MyMethod(CurrentVal)
    Assert.AreEqual(expected, actual)
    Assert.Inconclusive("Verify the correctness of this test method.")
End Sub

(Cross-posted at ASP.NET Forums)

回答1:

This could be a permissions issue.

If you're using the default directory (C:\users\\Documents\Visual Studio 2010\Projects), the app identity pool does not have permissions there. You'd have to create a project in something like C:\webs and make sure app pool identity has permission to the folder.

Refer to Rick Anderson's blog post at http://blogs.msdn.com/b/rickandy/archive/2011/04/22/test-you-asp-net-mvc-or-webforms-application-on-iis-7-in-30-seconds.aspx and see if that helps.



回答2:

If you have not done unit testing before, I would really recommend that you start by just testing the functionality of your classes as cleanly as possible. Try to break you you functionality into small pieces that can be tested individually without and dependencies to the web context.

Have a look at this question for an idea about What is unit testing Here is an MSDN Magazine article about testing You can also have a look at this Blog. The examples are using NUnit but the principal is the same if you are using MSTest.

I can also recommend Roy Osheroves Book Art of unit testing

In you case if the Member class does not have dependencies to web context you don't need the IIS and could instead just do something like this:

<TestMethod()> _
Public Sub MyMethodTest()
    Dim target = New Member() 
    Dim CurrentVal As Short = 0 ' TODO: Initialize to an appropriate value
    Dim expected As Short = 0 ' TODO: Initialize to an appropriate value
    Dim actual As Short
    actual = member.MyMethod(CurrentVal)
    Assert.AreEqual(expected, actual)
End Sub


回答3:

I ran into the same problem today. After some research, I found this thread which suggested I check my event log. Upon doing that, I discovered numerous errors similar to the following:

 (QTAgent32.exe, PID 12348, Thread 61) WebSites.GetWebServer: failed to
 create AspNetHelper:
 Microsoft.VisualStudio.Enterprise.Common.AspNetHelperException: The
 website metabase contains unexpected information or you do not have
 permission to access the metabase.  You must be a member of the
 Administrators group on the local computer to access the IIS metabase.
 Therefore, you cannot create or open a local IIS Web site.  If you
 have Read, Write, and Modify Permissions for the folder where the
 files are located, you can create a file system web site that points
 to the folder in order to proceed. --->
 System.Runtime.InteropServices.COMException: Unknown error
 (0x80005000)

That lead me to this blog post which seems to have resolved the issue.

I just needed to go to "Turn Windows features on or off" and add IIS 6 Management Compatibility and all four subcomponents. I'm running Windows 7 Home Premium which doesn't have the Windows Authentication option, but that didn't seem to be an issue. Give it a shot and see if that resolves the issue for you.



回答4:

You may need to enable "Use IIS" in the project properties, then click "Create Virtual Directory". Do you have IIS Express installed?