ASP.NET MVC2 - Render a View as a String

点点圈 提交于 2020-01-13 06:23:27

问题


How do you render a view as a string from a controller in MVC 2?

In MVC 1, I used CaptureActionHtml. I'm having the same problem with it as a similar question, but is there a way to do this without Rhink.Mocks?


回答1:


I've found Steve Sanderson's Integration Testing framework to work out very well for this.

Perusing one of the code samples from his own blog post gives you some idea of the capabilities of the framework, the assertions that you can perform against its output, etc:

[Test]
public void Root_Url_Renders_Index_View()
{
    appHost.SimulateBrowsingSession(browsingSession => {
        // Request the root URL
        RequestResult result = browsingSession.ProcessRequest("/");

        // You can make assertions about the ActionResult...
        var viewResult = (ViewResult) result.ActionExecutedContext.Result;
        Assert.AreEqual("Index", viewResult.ViewName);
        Assert.AreEqual("Welcome to ASP.NET MVC!", viewResult.ViewData["Message"]);

        // ... or you can make assertions about the rendered HTML
        Assert.IsTrue(result.ResponseText.Contains("<!DOCTYPE html"));
    });
}


来源:https://stackoverflow.com/questions/2746333/asp-net-mvc2-render-a-view-as-a-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!