4

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

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

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

这在系统测试中得到了体现,但我怎样才能更早地抓住它(最好是在构建服务器上)。

提前致谢

4

3 回答 3

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"));
    });
}
于 2011-01-20T14:58:49.260 回答
1

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

于 2011-01-20T15:49:31.750 回答
0

这是一个老问题,但如果有人还在寻找这个,你应该试试Matt Honeycutt 的 SpecsFor.Mvc

它不仅可以用来确保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("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");
        }
    }
} 
于 2016-03-11T22:45:49.763 回答