Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Preview 5 - Rendering A View To String For Testing

I was reading a post by Brad Wilson (http://bradwilson.typepad.com/blog/2008/08/partial-renderi.html) on the new ViewEngine changes to MVC Preview 5 and thought that it would be great to be able to render a view to string for use in tests. I get the impression from the article that it may be possible to achieve this but cannot figure out how.

I believe this would enable us to do away with some of our WatIn tests (which are slow and unreliable) as it would allow us to check that the View has rendered correctly by simply checking the string for expected values/text.

Has anyone implemented something like this?

like image 449
Chris Knight Avatar asked Sep 22 '08 14:09

Chris Knight


People also ask

What method is used to render HTML string in a view?

You can use the Html. Raw() method for that.

What is rendered in view of MVC?

A view renders the appropriate UI by using the data that is passed to it from the controller. This data is passed to a view from a controller action method by using the View method. The Views folder is the recommended location for views in the MVC Web project structure.

How do you render a partial view inside a view in MVC?

In order to add Partial View, you will need to Right Click inside the Controller class and click on the Add View option in order to create a View for the Controller.


1 Answers

It's tricky. What you have to do is set the Response.Filter property to a custom stream class that you implement. The MVC Contrib project actually has examples of doing this. I'd poke around in there.

like image 73
Haacked Avatar answered Oct 10 '22 10:10

Haacked