views:

979

answers:

2

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?

+5  A: 

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.

Haacked
A: 

Moreover testing, it can be useful for components such as HTML to PDF converters. These components usually uses 2 ways of transformation.

  • Passing a URL to the conversion method
  • Passing a HTML content (and you can optionally specify the baseUrl to resolve virtual paths)

I am using an Authorize filter inside the controller, so if I redirect to the URL the rendered HTML is the login page one (I use a custom authentication).

If I use Server.Execute(Url) to keep the context, the method fails (HttpUnhandledException: Error executing child request for /Template/Pdf/1.).

So I tried to retrieve the HTML of the rendered ViewResult but I didn't succeed.

labilbe