Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test the function "System.Web.Mvc.ViewEngines.Engines.FindPartialView"

I want to (unit)test the function System.Web.Mvc.ViewEngines.Engines.FindPartialView and check the correct return of the HTML Code.

But every time I start the unit test, it throws an "Object reference not set to an instance of an object" exception.

I've tried to debug through the .net framework source but the debugger gets disoriented and jumps randomly / breaks with no message.

Now I want to know what element I’ve missed in the FakeControllerContext and how to fix it.

Here's my Code:

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();
    }
}

And here's my test:

    [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);
            (...)
        }

For the mocking I’ve used Rhino.Mocks and the MvcFakes from Stephenwalther.com

like image 560
user1039490 Avatar asked Nov 10 '11 12:11

user1039490


1 Answers

I think that this thread can help you, you have to mock the ViewEngine then mock the FindPartialView call.

like image 182
Rarvick Avatar answered Oct 21 '22 08:10

Rarvick