【问题标题】:Avoiding missing views in ASP.NET MVC避免在 ASP.NET MVC 中丢失视图
【发布时间】:2011-06-12 12:28:27
【问题描述】:

在测试 ASP.NET MVC 2 应用程序时,我遇到了无法定位视图的问题。

查看代码后,我意识到视图的 aspx 文件尚未添加到源代码控制存储库中。在这个项目上,这很容易做到,因为我们使用 StarTeam 进行源代码控制,并且在签入时它不会显示新文件夹。这个视图是针对新控制器的,因此为它创建了一个新文件夹,因此错过了。

我们的构建服务器(使用 Hudson/MSBuild)没有注意到这一点,因为缺少 aspx 文件的代码仍然可以正常构建。我们的控制器单元测试测试了 ActionResults,显然在没有视图的情况下仍然可以通过。

这是在系统测试中发现的,但我怎样才能更早地发现它(最好是在构建服务器上)。

提前致谢

【问题讨论】:

  • 你可以使用一些 UI 测试框架,比如 WatiN。只需编写渲染视图的测试即可。

标签: asp.net-mvc hudson starteam


【解决方案1】:

你可以编写单元测试来测试实际的视图,然后如果单元测试在构建服务器上没有通过,你就知道你有问题。为此,您可以使用如下框架:
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"));
    });
}

【讨论】:

    【解决方案2】:

    您运行的是哪个版本的 StarTeam?在 StarTeam 2008(不确定首次添加此功能的时间)中,在选定的项目/视图中,您可以从菜单中选择 Folder Tree-&gt;Show Not-In-View Folders。这将显示您在本地磁盘上尚未添加到项目中的文件夹(它们将显示为白色的文件夹图标)。

    【讨论】:

    • 现在是 2006 年,但同样的功能仍然存在。我知道我可以做到,但问题是我忘了这样做,而且我的构建没有拿起它,所以我在 3 周后才发现它:(
    【解决方案3】:

    这是一个老问题,但如果有人还在寻找这个问题,你应该试试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

    这是从网站上截取的代码 sn-p,展示了如何使用它。

    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("test@user.com")
                    .Field(m => m.UserName).SetValueTo("Test User")
                    .Field(m => m.Password).SetValueTo("P@ssword!")
                    .Field(m => m.ConfirmPassword).SetValueTo("P@ssword!")
                    .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("test@user.com");
            }
    
            [Test]
            public void then_it_comes_from_the_expected_address()
            {
                SUT.Mailbox().MailMessages[0].From.Address.ShouldEqual("registration@specsfor.com");
            }
        }
    } 
    

    【讨论】:

      猜你喜欢
      • 2011-05-19
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      • 2015-06-06
      • 2014-03-18
      • 2022-10-08
      相关资源
      最近更新 更多