Avoiding Missing Views in ASP.NET MVC

When testing an ASP.NET MVC 2 application, I got into a problem when I could not find a view.

Looking at the code, I realized that the aspx file for the view was not added to the version control repository. In this project, which is quite easy to do, since we use StarTeam to manage the source code, and it does not show new folders during verification. This view was intended for the new controller, so a new folder was created for it, and therefore it was skipped.

Our build server (using Hudson / MSBuild) did not understand this, as the code still works fine with the absence of an aspx file. Our control modules validate the ActionResults action, which obviously still passes without viewing.

I managed to get this when testing the system, but how can I catch this earlier (ideally on the build server).

Thank you in advance

+4
source share
3 answers

You can write unit tests that check the actual look, and then if the unit test does not pass the build server, you know that you have a problem. For this you can use a framework, for example:
http://blog.stevensanderson.com/2009/06/11/integration-testing-your-aspnet-mvc-application/

With this, you can write unit tests like this (from a 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")); }); } 
+2
source

What version of StarTeam are you using? In StarTeam 2008 (not sure when this feature was first added) in the selected project / view, you can select from the menu Folder Tree->Show Not-In-View Folders . This will show the folders that you have on the local drive that have not been added to the project (they will be displayed with the folder icon, colored white).

+1
source

This is an old question, but if anyone else is looking for this, you should try SpecsFor.Mvc from Matt Honeycutt .

Not only can it be used to make sure Views properly included / added to the original control, it can even perform an integration test to make sure those Views valid.

Website Link: http://specsfor.com/SpecsForMvc/default.cshtml

Link to nuget package: https://www.nuget.org/packages/SpecsFor.Mvc/

Link to github: https://github.com/MattHoneycutt/SpecsFor

Here is a snippet of code taken from a website showing how to use it.

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

Source: https://habr.com/ru/post/1336322/


All Articles