2012-01-30 59 views
5

今天花了好幾個小時試圖針對ASP.NET項目編寫一些單元測試。它是Visual Studio 2010.ASP.NET單元測試Windows7/IIS7

使用帶有IIS7的Windows 7 Enterprise。

步驟我把爲:

  1. 增加了一個新的測試項目,以解決
  2. 打開一個類文件作爲網站的一部分(Member.vb)
  3. 右鍵單擊類文件中和「生成單元測試」
  4. 選擇我希望生成存根的方法,選擇添加到我的測試項目中,單擊確定
  5. 打開測試項目中生成的MemberTest.vb文件,單擊其中一個G enerated測試,請點擊「在CURENT上下文中運行測試」

當在與IIS6機器上我Windows XP Professional的這些精確的步驟,它工作正常。

但是在IIS7Windows 7企業版的機器上,我得到:

指定的URL(的 'http://本地主機/ MyProject的')不 對應於一個有效的目錄。配置爲在 的IIS中運行的測試IIS需要URL的有效目錄。該URL可能是 無效或可能不指向有效的Web應用程序。

所以發生了什麼事情,我可以確認我可以瀏覽到http://localhost/MyProject,它顯示完美。

我確定我錯過了Windows/IIS中的某種配置,但我真的很茫然。

生成的測試方法:

<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 

(跨張貼在ASP.NET Forums

回答

1

您可能需要啓用「使用IIS」在項目屬性,然後單擊「創建虛擬目錄」。你有沒有安裝IIS Express?

+0

不是IIS Express,但安裝了Windows 7 Enterprise的完整IIS。我已經查看了單元測試項目的每個項目屬性頁面,並且我看不到一個名爲「使用IIS」的選項,該選項在哪裏? – bgs264 2012-02-02 08:59:30

+0

的Web項目屬性:Web - >服務器部分 - >使用本地IIS服務器 – Mharlin 2012-02-03 08:25:18

+0

謝謝 - 這是設置正確,Web項目已經正常工作,只是測試項目不。 – bgs264 2012-02-03 08:53:46

3

如果您之前還沒有進行過單元測試,那麼我會建議您從儘可能乾淨地測試類的功能開始。嘗試將你的功能打破成小塊,可以單獨測試,無需依賴Web環境。

看一看這個問題的一個想法有關What is unit testing 這裏是一個MSDN Magazine article about testing 您也可以看看這個Blog。這些示例使用NUnit,但如果您使用MSTest,則主體是相同的。

我也可以推薦羅伊Osheroves書Art of unit testing

在你情況下,如果會員類沒有依賴於網絡環境下,你不需要IIS和可能,而不是僅僅做這樣的事情:

<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 
+0

謝謝你的觀點,但這不能回答這個問題。我已經試圖測試一個類的一種方法。然而,所有的類都是Web項目的一部分,而不是在單獨的類庫中,並且不能更改。 – bgs264 2012-02-03 08:51:13

+0

你不需要把它們分開來分開課程。重點是你可以單元測試只有Member.vb類,它是成員,如果它沒有任何依賴Web上下文,你不需要啓動IIS。 – Mharlin 2012-02-03 09:06:31

+0

我明白 - 但會員是在網站項目的上下文中定義的(在網站的App_Code中),而不在測試類的範圍內。我不想改變我的架構。我只是希望使Windows 7/IIS7機器以與Windows XP/IIS6設置相同的方式工作 - 它在哪裏工作。對不起,但這就是我的問題:) – bgs264 2012-02-03 11:52:03

4

這可能是一個權限問題。

如果您使用的是默認目錄(C:\ users \\ Documents \ Visual Studio 2010 \ Projects),那麼應用標識池在那裏沒有權限。您必須創建一個類似C:\ webs的項目,並確保應用程序池標識具有該文件夾的權限。

請參閱裏克安德森的博文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,看看是否有幫助。

+0

謝謝,它在c:\ projects中 - 我無法找到添加應用程序池標識權限的方法,但是我所做的是更改由應用程序池用於「網絡服務」的身份,並將此帳戶的權限添加到該文件夾​​。它沒有任何區別,它仍然是一樣的錯誤。 – bgs264 2012-02-09 10:21:37

+0

關於如何在IIS中設置AppPoolIdentity的更多信息,請參閱「保護資源」部分,以獲取文件夾的權限:[Application Pool Identities](http://learn.iis.net) /page.aspx/624/application-pool-identities/)。 – timamm 2012-02-09 18:20:45

+0

使用引用的鏈接時間給appPool標識權限。應用程序池標識比網絡服務更安全。 – RickAndMSFT 2012-02-18 01:37:33

3

今天我遇到了同樣的問題。經過一番調查後,我發現this thread建議我檢查我的事件日誌。一旦這樣做,我發現類似下面的許多錯誤:

(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) 

這導致我this blog post這似乎已經解決了這個問題。

我只需要轉到「打開或關閉Windows功能」並添加IIS 6管理兼容性和所有四個子組件。我正在運行沒有Windows身份驗證選項的Windows 7家庭高級版,但這似乎沒有問題。試試看,看看能否爲你解決問題。