测试函数“system . web . mvc . viewengine . engines . findpartial

本文关键字:viewengine engines findpartial mvc web 函数 system 测试 | 更新日期: 2023-09-27 18:13:57

我想(单元)测试函数System.Web.Mvc.ViewEngines.Engines.FindPartialView并检查HTML代码的正确返回。

但是每次我开始单元测试时,它都会抛出一个"Object reference not set to an instance of an object"异常。

我试图通过。net框架源代码调试,但调试器迷失方向,随机跳转/没有消息。

现在我想知道我在FakeControllerContext中错过了什么元素以及如何修复它。

这是我的代码:

public static string RenderPartialViewToString(string viewName, object model, ControllerContext controller)
{
    if (string.IsNullOrEmpty(viewName))
        viewName = controller.RouteData.GetRequiredString("action");
    controller.Controller.ViewData.Model = model;
    using (var sw = new StringWriter())
    {
        //"Error: ***.Shop.UnitTests.RenderStuffTest.RenderPartialViewToStringTest-Test method threw an exception: System.NullReferenceException – Object reference not set to an instance of an object"
        ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller, viewName);
        controller.Controller.ViewData.Model = model;
        controller.Controller.ViewBag.Part = true;
        var viewContext = new ViewContext(controller, viewResult.View, controller.Controller.ViewData,
                                      controller.Controller.TempData, sw);
        viewResult.View.Render(viewContext, sw);
        return sw.GetStringBuilder().ToString();
    }
}

这是我的测试:

    [TestMethod]
    public void RenderPartialViewToStringTest()
    {
            const string viewName = "_navi";
            var customersController = new ArticleController();
            customersController.ControllerContext = new FakeControllerContext(customersController)                                      {                                                           RouteData =
                                                        {
                                                            Route =
                                                                new Route(
                                                                "{language}/{controller}/{action}/{id}",
                                                                new MvcRouteHandler())
                                                            ,
                                                            RouteHandler = new MvcRouteHandler()
                                                        },
                                                };
            customersController.ControllerContext.RouteData.Values.Add("language", "German");
            customersController.ControllerContext.RouteData.Values.Add("controller", "Article");
            customersController.ControllerContext.RouteData.Values.Add("action", "Index");
            customersController.ControllerContext.RouteData.Values.Add("id", "");
            var model = (...);
            string actual = RenderStuff.RenderPartialViewToString(viewName, model, customersController.ControllerContext);
            (...)
        }

对于嘲弄,我使用了来自Stephenwalther.com的Rhino.MocksMvcFakes

测试函数“system . web . mvc . viewengine . engines . findpartial

我认为这个线程可以帮助你,你必须模拟ViewEngine然后模拟FindPartialView调用。