Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit test custom viewengine

I'm attempting to unit test a custom view engine I've written.

The intended functionality of the view engine is that it alters where the base RazorViewEngine looks when performing FindView

Here is my unit test

public void ViewEngineReturnsDependencyView()
{
    //Mock http request
    var mockRequest = new Mock<HttpRequestBase>();
    //Mock server variable
    NameValueCollection variables = new NameValueCollection();
    variables.Add("APPL_PHYSICAL_PATH", TEST_APPLICATION_PATH);
    mockRequest.Setup(r => r.ServerVariables).Returns(variables);

    //Mock http context
    var mockHttpContext = new Mock<HttpContextBase>();

    //Mock route
    mockHttpContext.Setup(c => c.Request).Returns(mockRequest.Object);
    var routeData = new RouteData();
    routeData.Values.Add("controller", "testController");
    routeData.Values.Add("action", "testAction");

    //Mock controller context
    var controllerContext = new testController().ControllerContext;
    controllerContext.HttpContext = mockHttpContext.Object;
    controllerContext.RouteData = routeData;
    var mockControllerContext = new ControllerContext(mockHttpContext.Object,
                        routeData, 
                        new Mock<ControllerBase>().Object);

    //Run find view
    viewEngine.FindView(mockControllerContext, "TestView", null, false);
}

Annoyingly viewEngine.FindView(...); throws an exception:

Test method ... threw exception: System.NullReferenceException: Object reference not set to an instance of an object. Result StackTrace:

at System.Web.WebPages.DisplayModeProvider.GetDisplayMode(HttpContextBase context)

at System.Web.Mvc.VirtualPathProviderViewEngine.GetPathFromGeneralName(ControllerContext controllerContext, List`1 locations, String name, String controllerName, String areaName, String cacheKey, String[]& searchedLocations)

at System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext, String[] locations, String[] areaLocations, String locationsPropertyName, String name, String controllerName, String cacheKeyPrefix, Boolean useCache, String[]& searchedLocations)

at System.Web.Mvc.VirtualPathProviderViewEngine.FindView(ControllerContext controllerContext, String viewName, String masterName, Boolean useCache)

at ...Mvc.CustomRazorViewEngine.FindView(ControllerContext controllerContext, String viewName, String masterName, Boolean useCache) in ...\Mvc\CustomRazorViewEngine.cs:line 85

at ...Tests.MVC.ViewEngine.ViewEngineReturnsDependencyView() in ...Tests\MVC\ViewEngine.cs:line 78

My question is, how do create the proper mocks to unit test RazorViewEngine.FindView()?

like image 731
MrJD Avatar asked Oct 24 '13 03:10

MrJD


1 Answers

The System.Web.WebPages.DisplayModeProvider.GetDisplayMode() method is using the HttpContext.Items property, you need to Mock that property as well.

Try:

mockHttpContext.Setup(c => c.Items).Returns(new Dictionary<object, object>());
like image 143
haim770 Avatar answered Oct 18 '22 23:10

haim770