c# – 在单元测试期间渲染视图 – ControllerContext.DisplayMode

前端之家收集整理的这篇文章主要介绍了c# – 在单元测试期间渲染视图 – ControllerContext.DisplayMode前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在研究一个生成大而复杂的报告的ASP.NET MVC 4 Web应用程序.我想编写单元测试来渲染视图,以确保视图不会爆炸,具体取决于模型:
[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?
 }

我已经在网络上看到了很多有关这方面的信息,但它要么反对测试竞争,要么只能在Web请求的上下文中使用.

> Arguing Against – Unit Testing the Views? – 结论是视图中没有逻辑,所以你只需要测试编译.我认为测试View是有价值的,以确保没有Null引用异常,显示正确的部分等.
> Web请求的上下文 – Render a view as a string – 这是为了呈现要在电子邮件中发送的视图.但是这种方法需要通过Web请求(即有效的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();
        }
    }

我正在构建控制器:

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只是公共类DummyController:Controller {}

提供View的路径,如何从Test项目将其呈现为HTML?或者更具体地说,我如何模拟出ControllerContext.DisplayMode?

解决方法

假设您完全分离了关注点,是否有必要实例化控制器?如果没有,那么也许您可以使用 RazorEngine来测试您的视图.
var contents = File.ReadAllText("pathToView"); 
var result = Razor.Parse(contents,model);
// assert here
原文链接:https://www.f2er.com/csharp/99192.html

猜你在找的C#相关文章