8

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?

Community
  • 1
  • 1
Miral
  • 5,968
  • 16
  • 57
  • 85
  • This is a dupe of unit testing the views? http://stackoverflow.com/questions/151794/unit-testing-the-views – Graviton Jun 17 '09 at 08:38
  • What would you want to test about your views anyway? What possible functionality they may have to unit test? If it's client side functionality, then unit test your scripts, if it's server side, then unit test your controllers... **Seriously: What's to test about views?** – Robert Koritnik Jun 14 '12 at 09:11

5 Answers5

9

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.

sduplooy
  • 14,340
  • 9
  • 41
  • 60
bh213
  • 6,343
  • 9
  • 43
  • 52
  • 3
    How did WatiN investment turn out? You likely develop whole system integration testing this way because there's no thing as unit testing with WatiN now is it? Did you maybe switch to Selenium or do you use still WatiN? Anyway. Let us know the outcome of this *gymnastics*... I'm likely not the only person being interested. – Robert Koritnik Jun 14 '12 at 08:23
7

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
  • 6,573
  • 4
  • 29
  • 41
  • 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 Jun 17 '09 at 08:51
  • 2
    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 Jun 17 '09 at 08:54
  • 3
    Url.Content("~"/something) is worth writing some automated tests for. verification those file still exist or are resolved properly is worthwhile automation. – Maslow Jan 28 '10 at 17:07
  • 1
    I agree with Maslow, there should be automated tests for this, but I would suggest that this is done further out than unit testing (in a strict sense). Perhaps using something like Specflow, or even further out with a selenium test to check that a link points where you expect and can be followed. – Mark Dickinson Jun 18 '12 at 08:41
3

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
  • 383
  • 1
  • 7
3

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
  • 4,465
  • 1
  • 26
  • 19
  • That would more likely be system integration testing not just basic integration testing. The big bang style... Just wanted to clarify on that... – Robert Koritnik Jun 14 '12 at 09:08
0

For those who do not see the value in testing views.... How can you be sure that the view has the right attributes on elements, or that it is bound correctly?

Many answer "at a higher level" (such as running the site and using tools such as selenium or equivalent).

However these techniques make it virtually impossible to prove that the source of the error is in the view itself and also require massive changes to the server side code so that the views can be rendered in a targeted manner.

David V. Corbin
  • 344
  • 1
  • 10