views:

935

answers:

6

Duplicate: Unit Testing the Views?

Is there any way to unit test View? I am sure that we can test Model & Controller but don't know how to unit test View?

Is that worth testing View?

+3  A: 

Haven't views abandoned code behind now? So what are you going to test? If you are testing the controller, then you just need a succesful view result to show that the view works. Rather than going to the trouble of pre compiling views or whatever, this will start to drag any sizeable project down in terms of continuous integration, and build.

Mark Dickinson
I am surely not going to have any .aspx.cs file in my project, but we do write some sort of code within server tags <% %> in the aspx page
Miral
Thinkg is, this code should be so trivial that it doesn't need any detailed testing. If you have lots of conditionals and loops then you need to look at changing your controller to provide a better structured model. I hope this helps, MVC is definately worth the trouble :)
Mark Dickinson
Url.Content("~"/something) is worth writing some automated tests for. verification those file still exist or are resolved properly is worthwhile automation.
Maslow
+2  A: 

From what I have read (in Pro ASP.NET MVC Framework by Steven Sanderson), views are not considered worth testing. ASP.NET MVC viewes can be generated using various engines, e.g. the default lightweight ASPX, or for example http://www.stringtemplate.org/. For ASPX output you might run some HTML syntax checker tool, and for other view engines the fact that the views compile successfully should be a good enough test ;)

Slavcho
A view may compile while still failing to locate it's content references.
Maslow
A: 

I do not see the point of unit testing the views, since they don't contain much logic. You can however to some integration testing/UI testing using a tool like WatiN.

Example of a test written in WatiN:

[Test]
public void SearchForWatiNOnGoogle()
{
   using (IE ie = new IE("http://www.google.com"))
   {
      ie.TextField(Find.ByName("q")).TypeText("WatiN");
      ie.Button(Find.ByName("btnG")).Click();

      Assert.IsTrue(ie.ContainsText("WatiN"));
  }
}

You should not try to test everything using tool like this. Select some key functionality of the application, and write test for them.

BengtBe
+1  A: 

You can enable compilation of MVC views. That helps a lot. Otherwise, I don't think it is worth it. After all, the there are only two things that you are interested in. Does view compile and do you get any exceptions (null, out of bounds exceptions, or similar)?

There are some folks who claim that you should not include any logic in view. Write helpers for anything. In that case, compilation is pretty much everything you'll want.

We decided to invest into WatiN testing. It tests views and it tests the whole app at the same time. Has some nice helpers, but requires constant maintainance.

bh213
A: 

Does anyone know how to unit test view rendered HTML output. I consider compilation not enough, for example I would like to check if the rendered HTML is valid XHTML.

Jeroen Bernsen
To post a question on Stack Overflow you should click the "Ask Question" button in the top right corner, not post it as an answer to another question.
Martin Liversage
-1 this is a question, not an answer.
Maslow
A: 

At this moment I am finetuning a solution for this 'problem' which is based on rendering the page using code that is based on ApplicationHost.CreateApplicationHost. So far it works with Get, Post and Windows authentication. I am planing to blog about it in about a week or so.

Edwin Vermeer