5

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)

bgs264
  • 4,572
  • 6
  • 38
  • 72

4 Answers4

4

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.

timamm
  • 922
  • 7
  • 20
  • Thanks, it was in c:\projects - I could not find a way to add the permission for the application pool identity, however what I have done is to change the identity used by the application pool to 'Network service' and added permissions for this account to the folder. It didn't make any difference, it was still the same error. – bgs264 Feb 09 '12 at 10:21
  • For what it's worth, here's a link on more info on how to set the AppPoolIdentity in IIS (see the Securing Resources section for giving it permissions on a folder): [Application Pool Identities](http://learn.iis.net/page.aspx/624/application-pool-identities/). – timamm Feb 09 '12 at 18:20
  • Use the link time cited to give the appPool idenity permission. App pool identities are more secure than network service. – RickAndMSFT Feb 18 '12 at 01:37
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.

Community
  • 1
  • 1
3

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
Community
  • 1
  • 1
Mharlin
  • 1,697
  • 16
  • 24
  • Thanks for the points but this doesn't answer the question. I am already trying to test just one method of one class. However all the classes are part of the web project, not in a separate class library, and that can't be changed. – bgs264 Feb 03 '12 at 08:51
  • You don't need to break them out to separate classes. The point is that you can unit test only the Member.vb class and it's members if it doesn't have any dependencies to web context you don't need to fire up the IIS. – Mharlin Feb 03 '12 at 09:06
  • I understand- but Member is defined within the context of the web site project (in App_Code of the website), and not within the scope of the testing class. I don't wish to change my architecture. I simply wish to make the Windows 7/IIS7 machine work in the same way as the Windows XP/IIS6 setup - where it works. Sorry, but that's what my question is :) – bgs264 Feb 03 '12 at 11:52
  • This answer helped me, so even if it is a year late. The point Mharlin is making (I think): if you can test it **without** _HostType_ and _UrlToTest_ attributes (his code is wrong in omitting the `Member_Accesor`, needed for testing private functions) then you still tested it and you did validate the method. Just give it a try and mark this as the answer... – Albert Mar 14 '13 at 16:38
1

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

Lilith River
  • 16,204
  • 2
  • 44
  • 76
  • Not IIS Express, but full IIS installed with Windows 7 Enterprise. I've looked on every page of the project properties for the unit testing project and I cannot see an option called "Use IIS", where is this option? – bgs264 Feb 02 '12 at 08:59
  • the web project properties: Web -> Server section -> Use Local IIS Server – Mharlin Feb 03 '12 at 08:25
  • Thanks - This is set correctly and the web project already works fine, just the testing project which doesn't. – bgs264 Feb 03 '12 at 08:53