asp.net-mvchudsonstarteam

Avoiding missing views in ASP.NET MVC


When testing an ASP.NET MVC 2 application I hit a problem when a view could not be located.

Looking at the code I realised that the aspx file for the view had not been added to the source control repository. On this project that's quite easy to do as we use StarTeam for source control and it doesn't show new folders when checking in. This view was for a new controller and so a new folder was created for it and it was therefore missed.

Our build server (using Hudson/MSBuild) didn't pick up on this, as the code still builds fine with the aspx file missing. Our controller unit tests test the ActionResults which obviously still pass without the view there.

This got picked up in system testing but how can I catch this earlier (ideally on the build server).

Thanks in advance


Solution

  • You can write unit tests that test the actual view, and then if the unit test doesn't pass on the build server, you know you have a problem. To do this, you can use a framework such as this:
    http://blog.stevensanderson.com/2009/06/11/integration-testing-your-aspnet-mvc-application/

    With this you can write unit tests such as this (from the post)

    [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"));
        });
    }