[ACCEPTED]-unit testing asp mvc view-unit-testing

Accepted answer
Score: 17

There are 3 options:

  1. You want to unit test the code in the view. In this case, you have to move the code to the controller, because it's not the responsibility of the view to have this code.
  2. You want to be sure the view is actually shown in the browser. Use an browser UI testing tool like waitin or selenium. This does not create an isolated test of the view, but of large parts of your application. That sounds like an advantage, but is there any value in an isolated test of a view?
  3. You want to test that code in your view is compilable code. Then compile the code. This can be done in a unit test, by manually calling view.compile or by turning on the view compiler in the build process.

0

Score: 5

It is possible to test your views via unit 7 test using Visual Studio Test Tools, but 6 only the values, created by the controller 5 (e.g. values in ViewBag: ViewBag.message = "My message.") or the name of 4 the rendered view:

[TestMethod]
public void MyActionTest()
{
    // Arrange
    var lController = new HomeController();

    // Act
    var lResult = lController.MyAction() as ViewResult;

    // Assert
    Assert.IsTrue(lResult.ViewBag.message == "My message.", "Wrong Message in Viewbag.");
    Assert.IsTrue(lResult.ViewName == "MyView", "Incorrect view.");
}

If you want to auto-test 3 your entire view inclunding HTML, I recommend 2 Selenium IDE as fast and simple solution and Selenium Web Driver for cross-browser-tests 1 and experts.

Score: 3

Unit tests normally never test UI because 15 it's simply too brittle to do so.

While you could argue 14 that a minum test would be that the View 13 doesn't crash with an exception when we 12 attempt to render it, this would also be 11 about the only unit test we could actually 10 write for a View (ASP.NET MVC, WPF, Windows 9 Forms, etc. - it doesn't really matter).

The 8 next thing you (or your customer) would 7 want to test is that the View is rendered 6 correctly, and you can't reliably do this 5 with an automated test. What it all boils 4 down to is that Views are better tested 3 by Visual Inspection because the return of investment on that is simply better 2 than trying to develop and maintain automated 1 UI tests.

Score: 2

You should look at the web testing tools 8 in the MS Test suite or one of a number 7 of other web testing tools, like Selenium to test 6 the views, if you need automated tests for 5 these. I think you'll find it easier than 4 adapting a unit testing framework.

Full disclosure: I 3 still test my UI by hand. I haven't found 2 enough benefit to outweigh the cost of learning, setting 1 up, and maintaining web tests.

Score: 0

My advice is to not bother to extensively 6 test your views. It is hard, and If you 5 are doing things right, there will not be 4 much logic in them anyway.

That said, WatiN is 3 a good tool for automated browser testing 2 - not exactly what you wanted, but it works 1 well.

Score: 0

It's fairly easy to test the view in an 9 ASP.NET MVC page.

I walk you through step-by-step in a YouTube video. Here's an outline of 8 the required steps:

  1. You need to create a 7 unit test in the controller. This allows to to easily call 6 the rendered result. For example:

    public ActionResult TestScenario()
    {
         // setup some models
         return View("Page", model);
    }
    
  2. Your unit 5 test needs to call that ControllerAction, and 4 verify the result. In ApprovalTests this 3 would be:

    MvcApprovals.VerifyMvcPage(new MyController().TestScenario);
    

    This is all rather straight-forward 2 in ApprovalTests (www.approvaltests.com 1 or nuget).

More Related questions