views:

162

answers:

4

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. http://stackoverflow.com/questions/383192/compile-views-in-asp-net-mvc

A: 

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

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

tvanfosson
A: 

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

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

Gabe Moothart
A: 

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.

Mark Seemann
+2  A: 

There are 3 options:

  1. You want to unit test to 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.
Paco
Could you possibly elaborate on point 3?
Paul Creasey
Acceoted for point 3 :)
Paul Creasey