views:

286

answers:

3

Hi,

I am really missing heavily the ability to test Views independently of controllers. The way RSpec does it.

What I want to do is to perform assertions on the rendered view (where no controller is involved!). In order to do so I should provide required Model, ViewData and maybe some details from HttpContextBase (when will we get rid of HttpContext!).

So far I have not found anything that allows doing it. Also it might heavily depend on the ViewEngine being used.

List of things that views might contain are:

  • Partial views (may be nested deeply).
  • Master pages (or similar in other view engines).
  • Html helpers generating links and other elements.
  • Generally almost anything in a range of common sense :) .

Also please note that I am not talking about client-side testing and thus Selenium is just not related to it at all. It is just plain .NET testing.

So are there any options to actually do the testing of views?

Thanks,
Dmitriy.

+1  A: 

You may want to check out the UI Test Helpers that Eric Hexter and the guys with MVCContrib are working on. I haven't had a chance to look at it in depth, but it may help you. I found this link that shows some of the syntax: http://codepaste.net/cw8ie4

I would be interested to know what you find out as I will be doing this pretty soon also.

Brian McCord
Brian, the `MvcContrib.TestHelper.Ui` allows integration testing using a framework like WatiN or Selenium. I don't need that. I really just need to render the view (passing required model to i) and assert on its content (preferably using CSS/XPath selectors).
Dmytrii Nagirniak
I see. But, that leads me to another question. What would you be testing at that point? It seems to me that you would be testing that the render engine works. Where is the point that you just have to trust the framework?
Brian McCord
I will be testing the generated HTML, ensuring the correct FORM, INPUT and other elements are presented and valid. This way I can be sure that, for example, the pages won't break after a designer would have applied a new design.
Dmytrii Nagirniak
A: 

Interested to know if you find anything for .Net that does this. Our current app is WPF, but we are stuck with trusting Cucumber to touch our Views in all our Features... so yeah, that sucks. Hope you find something and update us.

Sean B
+1  A: 

The main issue with testing full views is that the asp.net view engine calls Response.Write in the supplied context / and not on the supplied writer.

The above is not the case for testing partial views, so for those you can use this solution: http://www.brightmix.com/blog/renderpartial-to-string-in-asp-net-mvc/

There are other view engines that do allow you to test the view i.e. Spark.

ps. the concept in asp.net mvc is that you should be able to test the view by using the ViewEngine, but as I understand the asp.net mvc team didn't work around the existing asp.net engine restrictions to be able to do so for their view engine.

eglasius
I will certainly look into Spark. I used NHaml before, but not sure testing can be done with it. Thanks for the answer.
Dmytrii Nagirniak