开发者

Test Views in ASP.NET MVC2 (ala RSpec)

开发者 https://www.devze.com 2022-12-29 22:31 出处:网络
I am really missing heavily the ability to test Views independently of controllers. The way RSpec does it.

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 neste开发者_JAVA百科d 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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号