8

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

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
user1039490
  • 81
  • 1
  • 2
  • 1
    Maybe it's trying to read `HttpContext.Current` from the thread which won't be set. Why do you want to unit test this? Try downloading the MVC source - you should find Microsoft's existing unit test in there. – Rup Nov 10 '11 at 14:25
  • I've got a got a view for an article. This View is used for the website and for emails with article recommendation. But there is little difference in the html between these two calls. Now I want to write a test that checks the view for these two calls. Maybe I didn’t found the right unit test in the mvc project files but I don’t get a test that actually renders the html – user1039490 Nov 11 '11 at 10:35

1 Answers1

1

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

Community
  • 1
  • 1
Rarvick
  • 21
  • 4
  • 3
    IIRC it's the FindPartialView call he's intested in - he wants to mock up enough of the surrounding classes so that he can run the real FindPartialView code, not mock it. – Rup Mar 01 '12 at 10:49