Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test Views in ASP.NET MVC2 (ala RSpec)

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.

like image 948
Dmytrii Nagirniak Avatar asked May 06 '10 12:05

Dmytrii Nagirniak


2 Answers

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.

like image 80
eglasius Avatar answered Oct 20 '22 17:10

eglasius


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.

like image 1
Brian McCord Avatar answered Oct 20 '22 17:10

Brian McCord