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
I think that this thread can help you, you have to mock the ViewEngine then mock the FindPartialView call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With