2011-01-20 118 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]"); 
     } 
    } 
}