2011-01-20 101 views
4

在測試ASP.NET MVC 2應用程序時,我無法找到視圖時遇到了問題。避免在ASP.NET MVC中丟失視圖

看着代碼,我意識到視圖的aspx文件還沒有被添加到源代碼控制庫中。在這個項目中,我們使用StarTeam進行源代碼控制非常容易,並且在簽入時不顯示新文件夾。此視圖用於新控制器,因此爲它創建了一個新文件夾,因此錯過了它。

我們的構建服務器(使用Hudson/MSBuild)沒有選擇這個,因爲代碼在aspx文件丟失的情況下仍然正常。我們的控制器單元測試測試顯然仍然沒有在那裏看到的ActionResults。

在系統測試中得到了答案,但是我怎麼能早點(在構建服務器上)理解這一點。

在此先感謝

+0

你可以涉及一些像WatiN這樣的UI測試框架。只需編寫視圖呈現的測試。 – 2011-01-20 14:54:34

回答

2

您可以編寫單元測試,測試實際視圖,然後如果單元測試沒有通過構建服務器上,你知道你有問題。要做到這一點,你可以用一個框架,像這樣的:
http://blog.stevensanderson.com/2009/06/11/integration-testing-your-aspnet-mvc-application/

有了這個,你可以編寫單元測試,像這樣的(從後)

[Test] 
public void Root_Url_Renders_Index_View() 
{ 
    appHost.SimulateBrowsingSession(browsingSession => { 
     // Request the root URL 
     RequestResult result = browsingSession.ProcessRequest("/"); 

     // You can make assertions about the ActionResult... 
     var viewResult = (ViewResult) result.ActionExecutedContext.Result; 
     Assert.AreEqual("Index", viewResult.ViewName); 
     Assert.AreEqual("Welcome to ASP.NET MVC!", viewResult.ViewData["Message"]); 

     // ... or you can make assertions about the rendered HTML 
     Assert.IsTrue(result.ResponseText.Contains("<!DOCTYPE html")); 
    }); 
} 
+0

很好的答案... – Paddy 2011-01-20 15:52:38

1

您運行的是什麼版本的StarTeam的?在選定的項目/視圖中,StarTeam 2008(不確定何時首次添加此功能時),您可以從菜單Folder Tree->Show Not-In-View Folders中進行選擇。這將顯示您在本地磁盤上尚未添加到項目中的文件夾(它們將以文件夾圖標顯示爲白色顯示)。

+0

現在是2006年,但同樣的功能在那裏。我知道我可以做到這一點,但問題是我忘了這樣做,我的構建沒有拿起它,所以我只在3周後發現它:( – 2011-01-20 16:00:10

0

這是一個老問題,但如果有人仍然在尋找這個,你應該嘗試SpecsFor.Mvc by Matt Honeycutt

不僅可以用它來確保Views被正確地包含/添加到源代碼管理中,它甚至可以執行集成測試以確保那些Views有效。

鏈接到它的網站:http://specsfor.com/SpecsForMvc/default.cshtml

鏈接到NuGet包:https://www.nuget.org/packages/SpecsFor.Mvc/

鏈接到github上:https://github.com/MattHoneycutt/SpecsFor

這裏是展示如何使用它的網站採取了代碼段。

public class UserRegistrationSpecs 
{ 
    public class when_a_new_user_registers : SpecsFor<MvcWebApp> 
    { 
     protected override void Given() 
     { 
      SUT.NavigateTo<AccountController>(c => c.Register()); 
     } 

     protected override void When() 
     { 
      SUT.FindFormFor<RegisterModel>() 
       .Field(m => m.Email).SetValueTo("[email protected]") 
       .Field(m => m.UserName).SetValueTo("Test User") 
       .Field(m => m.Password).SetValueTo("[email protected]!") 
       .Field(m => m.ConfirmPassword).SetValueTo("[email protected]!") 
       .Submit(); 
     } 

     [Test] 
     public void then_it_redirects_to_the_home_page() 
     { 
      SUT.Route.ShouldMapTo<HomeController>(c => c.Index()); 
     } 

     [Test] 
     public void then_it_sends_the_user_an_email() 
     { 
      SUT.Mailbox().MailMessages.Count().ShouldEqual(1); 
     } 

     [Test] 
     public void then_it_sends_to_the_right_address() 
     { 
      SUT.Mailbox().MailMessages[0].To[0].Address.ShouldEqual("[email protected]"); 
     } 

     [Test] 
     public void then_it_comes_from_the_expected_address() 
     { 
      SUT.Mailbox().MailMessages[0].From.Address.ShouldEqual("[email protected]"); 
     } 
    } 
}