2012-07-04 41 views
2

我在Visual Studio 2010中創建了一個默認MVC 4.0 Internet Web應用程序。在其中一個自動生成的家庭控制器方法中,我右鍵單擊並創建了一個單元測試。我選擇一個單獨的/新的項目。測試代碼如下:VS 2010 mvc控制器的默認單元測試屬性是原因測試無法運行,但爲什麼?

/// <summary> 
    ///A test for Index 
    ///</summary> 
    // TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example, 
    // http://.../Default.aspx). This is necessary for the unit test to be executed on the web server, 
    // whether you are testing a page, web service, or a WCF service. 
    [TestMethod()] 
    [HostType("ASP.NET")] 
    [AspNetDevelopmentServerHost("E:\\Backup 20080915\\Projects\\Writing Projects\\Blogs\\31a\\QUnitMVC\\jQueryMvcWebApp\\jQueryMvcWebApp", "/")] 
    [UrlToTest("http://localhost:61524/")] 
    public void IndexTest() 
    { 
     HomeController target = new HomeController(); // TODO: Initialize to an appropriate value 
     ActionResult expected = null; // TODO: Initialize to an appropriate value 
     ActionResult actual; 
     actual = target.Index(); 
     Assert.AreEqual(expected, actual); 
     Assert.Inconclusive("Verify the correctness of this test method."); 
    } 

如果我運行測試,完全由VS寫的,它失敗:

Failed IndexTest jQueryMvcWebApp.Test.HomeControllerTest.IndexTest The Web request 'http://localhost:61524/' 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_IndexTest.html' with the test results; typically this file can be opened with a Web browser to view its contents.  

如果我註釋掉以下3個屬性,測試運行正常:

[HostType("ASP.NET")] 
[AspNetDevelopmentServerHost("E:\\Backup 20080915\\Projects\\Writing Projects\\Blogs\\31a\\QUnitMVC\\jQueryMvcWebApp\\jQueryMvcWebApp", "/")] 
[UrlToTest("http://localhost:61524/")] 

所以我的問題是,他們爲什麼默認設置,什麼時候應該讓他們取消註釋?

回答

0

如果您仔細閱讀錯誤消息,您會看到原因。您實際上並未加載可成功進入Load事件的ASP.NET頁面。你正在運行MVC,它確實沒有「頁面」。我完全單元測試每一個控制器/操作,我從來沒有嘗試過使用這些屬性。可能是Visual Studio測試中的任何內容,以便爲任何.net Web應用程序默認這些屬性 - 當它真的只適用於Web窗體應用程序時。