Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unit testing asp mvc view

How can i unit test the view of an ASP MVC application?

I have tried the mvc contrib test helper...

 _controller.Index().AssertViewRendered();

but this doesn't actually test the view.

for example i can happily insert some bogus code in the view, and get the dreaded yellow screen of death, without my unit test ever knowing about it.

Is there any estabilished method of doing this? Do i need a mock out the view engine? Does mvccontrib cater to this?

I would have thought this would be a very common requirement, but i can't find much about it!

Thanks

EDIT What i'm really after is compile time checking, to ensure that model changes don't impact the view.

this question contained the instructions to enable build time view compilation which is sufficient for me for now. Compile Views in ASP.NET MVC

like image 512
Paul Creasey Avatar asked Feb 22 '10 22:02

Paul Creasey


People also ask

How do I unit test a view controller?

To make your Unit Test load the UIViewController that has an interface XIB file is even easier. Create an instance on UIViewController that uses XIB file. Make sure though that the view controller and the XIB files have the same names. Call the loadViewIfNeeded() method to make the viewDidLoad() execute.

Why unit testing is easy in MVC?

We create the data access logic in a separate class, or set of classes, called a repository, with the responsibility of persisting the application's business model. As the Repository Pattern is useful for decoupling entity operations from presentation, it allows easy mocking and unit testing.


3 Answers

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.
like image 100
Paco Avatar answered Oct 17 '22 05:10

Paco


It is possible to test your views via unit test using Visual Studio Test Tools, but only the values, created by the controller (e.g. values in ViewBag: ViewBag.message = "My message.") or the name of 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 your entire view inclunding HTML, I recommend Selenium IDE as fast and simple solution and Selenium Web Driver for cross-browser-tests and experts.

like image 31
Simon Avatar answered Oct 17 '22 07:10

Simon


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

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

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

like image 3
Mark Seemann Avatar answered Oct 17 '22 06:10

Mark Seemann