小编use*_*490的帖子

测试函数"System.Web.Mvc.ViewEngines.Engines.FindPartialView"

我想(单元)测试函数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, …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing rhino-mocks razor asp.net-mvc-3

8
推荐指数
1
解决办法
3878
查看次数

标签 统计

asp.net-mvc-3 ×1

c# ×1

razor ×1

rhino-mocks ×1

unit-testing ×1