MVC 预览 5 - 将视图渲染为字符串以进行测试
我正在阅读 Brad Wilson 的一篇文章 (http://bradwilson.typepad .com/blog/2008/08/partial-renderi.html)关于新 ViewEngine 对 MVC Preview 5 的更改,并认为能够将视图渲染为字符串以供测试使用会很棒。 我从这篇文章中得到的印象是,也许可以实现这一目标,但不知道如何实现。
我相信这将使我们能够取消一些 WatIn 测试(这些测试很慢且不可靠),因为它允许我们通过简单地检查字符串的预期值/文本来检查视图是否已正确呈现。
有人实施过这样的事情吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此外,它对于 HTML 到 PDF 转换器等组件也很有用。
这些组件通常使用2种转换方式。
我在控制器内使用授权过滤器,因此如果我重定向到 URL,则呈现的 HTML 是登录页面一(我使用自定义身份验证)。
如果我使用 Server.Execute(Url) 来保留上下文,该方法将失败(HttpUnhandledException:执行 /Template/Pdf/1 的子请求时出错。)。
所以我尝试检索渲染的 ViewResult 的 HTML,但没有成功。
Moreover testing, it can be useful for components such as HTML to PDF converters.
These components usually uses 2 ways of transformation.
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.
我想这就是你所需要的。 RenderPartialToString 函数将以字符串形式返回控制器。 我从这里。
I think here is what you need. The
RenderPartialToString
function will return the controller as a string. I get it from here.这很棘手。 您需要做的是将 Response.Filter 属性设置为您实现的自定义流类。 MVC Contrib 项目实际上有这样做的示例。 我会在那里闲逛。
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.