开发者

ASP.NET MVC2 - Render a View as a String

开发者 https://www.devze.com 2022-12-28 14:20 出处:网络
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 sim开发者_JS百科ilar question, but is there a way to do

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 sim开发者_JS百科ilar question, but is there a way to do this without Rhink.Mocks?


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"));
    });
}
0

精彩评论

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

关注公众号