gpt4 book ai didi

c# - 在单元测试期间呈现 View - ControllerContext.DisplayMode

转载 作者:太空狗 更新时间:2023-10-29 20:30:30 28 4
gpt4 key购买 nike

我正在开发一个生成大型复杂报告的 ASP.NET MVC 4 Web 应用程序。我想编写渲染 View 的单元测试,以确保 View 不会因模型而崩溃:

 [Test]
public void ExampleTest(){
var reportModel = new ReportModel();

var reportHtml = RenderRazorView(
@"..\..\Report.Mvc\Views\Report\Index.cshtml",
reportModel);

Assert.IsFalse(
string.IsNullOrEmpty(reportHtml),
"View Failed to Render!");
}

public string RenderRazorView(string viewPath, object model){
//WHAT GOES HERE?
}

我在网络上看到了很多关于这个的信息,但它要么反对测试 vies,要么只能在网络请求的上下文中使用。

  • 反对 - Unit Testing the Views? - 结论是 View 中应该没有逻辑,因此您只需要测试编译。我认为测试 View 以确保不存在空引用异常、显示正确的部分等是有值(value)的。
  • Web 请求的上下文 - Render a view as a string - 这是为了呈现要在电子邮件中发送的 View 。但这种方法需要通过网络请求(即有效的 HttpContextBase)调用。

我一直在努力适应Render a view as a string使用 Mocked HttpContextBase,但在使用 Mocked ControllerContext 时遇到问题:

Object reference not set to an instance of an object. at System.Web.WebPages.DisplayModeProvider.GetDisplayMode(HttpContextBase context) at System.Web.Mvc.ControllerContext.get_DisplayMode() at System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext, String[] locations, String[] areaLocations, String locationsPropertyName, String name, String controllerName, String cacheKeyPrefix, Boolean useCache, String[]& searchedLocations)

这是我目前的代码:

    public string RenderRazorView(string viewPath, object model)
{
var controller = GetMockedDummyController();

//Exception here
var viewResult =
ViewEngines.Engines.FindView(controller.ControllerContext, "Index", "");

using (var sw = new StringWriter())
{
var viewContext =
new ViewContext(
controller.ControllerContext,
viewResult.View,
new ViewDataDictionary(model),
new TempDataDictionary(),
sw);

viewResult.View.Render(viewContext, sw);

return sw.ToString();
}
}

我正在构建 Controller :

    private Controller GetMockedDummyController()
{
var HttpContextBaseMock = new Mock<HttpContextBase>();
var HttpRequestMock = new Mock<HttpRequestBase>();
var HttpResponseMock = new Mock<HttpResponseBase>();
HttpContextBaseMock.SetupGet(x => x.Request).Returns(HttpRequestMock.Object);
HttpContextBaseMock.SetupGet(x => x.Response).Returns(HttpResponseMock.Object);

var controller = new DummyController();

var routeData = new RouteData();
routeData.Values.Add("controller", "Dummy");

controller.ControllerContext =
new ControllerContext(
HttpContextBaseMock.Object,
routeData,
controller);

controller.Url =
new UrlHelper(
new RequestContext(
HttpContextBaseMock.Object,
routeData),
new RouteCollection());

return controller;
}

DummyController 只是public class DummyController : Controller {}

问题

给出一个 View 的路径,我怎样才能将它从一个测试项目渲染成 HTML?或者更具体地说,如何模拟 ControllerContext.DisplayMode

最佳答案

假设您完全分离关注点,是否有必要实例化 Controller ?如果没有,那么也许您可以使用 RazorEngine测试你的观点。

var contents = File.ReadAllText("pathToView"); 
var result = Razor.Parse(contents,model);
// assert here

关于c# - 在单元测试期间呈现 View - ControllerContext.DisplayMode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28087796/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com