Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 4 Mocking HttpContext - how to mock DisplayModeProvider

I was currently using Scott Hanselmans HTTP context mock for unit testing. This worked well for MVC 3 and never looked back, I used it for testing calls for the following code.

public class PartialViewRenderer : IPartialViewRenderer
{
    public string Render(Controller controller, string viewName, object model)
    {
        if (string.IsNullOrEmpty(viewName))
            viewName = controller.ControllerContext.RouteData.GetRequiredString("action");

        controller.ViewData.Model = model;

        using (StringWriter sw = new StringWriter())
        {
            ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
            ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View,
                                                      controller.ViewData, controller.TempData, sw);
            viewResult.View.Render(viewContext, sw);

            return sw.GetStringBuilder().ToString();
        }
    }
}

When I first converted my app to MVC 4 I ran into problems, it was getting runtime exceptions. So I tried going through and fixing what needed fixing, which landed me on changing the following methods on Hanselmans MockHelpers: (I basically changed the HttpContext.Items to get returned since it was blowing an exception of "null")

 public static HttpContextBase FakeHttpContext()
    {
        var context = new Mock<HttpContextBase>();
        var request = new Mock<HttpRequestBase>();
        var response = new Mock<HttpResponseBase>();
        var session = new Mock<HttpSessionStateBase>();
        var server = new Mock<HttpServerUtilityBase>();
        var cookies = new HttpCookieCollection();
        var items = new ListDictionary();

        request.Setup(r => r.Cookies).Returns(cookies);
        response.Setup(r => r.Cookies).Returns(cookies);

        context.Setup(ctx => ctx.Items).Returns(items);

        context.SetupGet(ctx => ctx.Request).Returns(request.Object);
        context.SetupGet(ctx => ctx.Response).Returns(response.Object);
        context.SetupGet(ctx => ctx.Session).Returns(session.Object);
        context.SetupGet(ctx => ctx.Server).Returns(server.Object);

        return context.Object;
    }

 public static void SetFakeControllerContext(this Controller controller, RouteData route)
    {
        var httpContext = FakeHttpContext();

        ControllerContext context = new ControllerContext(new RequestContext(httpContext, route), controller);

        controller.ControllerContext = context;
    }

Here a very simple nUnit test I have to try and nail down what changes I need to make to this mock of http context (I havent even put in the asserts yet)

    [Test]
    public void test()
    {
        _contactsController = _container.Resolve<ContactsController>();

        var route = new RouteData();

        route.Values.Add("controller", "ContactsController");
        route.Values.Add("action", "GetEditContactDetailsDialog");

        _contactsController.SetFakeControllerContext(route);

        var result = _contactsController.GetEditContactDetailsDialog("1");
    }

Now when I run this test it bombs on the PartialViewRenderer.Render call on line ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName); Here is the following stacktrace.

at System.Web.WebPages.DisplayModeProvider.<.ctor>b__2(HttpContextBase context) at System.Web.WebPages.DefaultDisplayMode.CanHandleContext(HttpContextBase httpContext) at System.Web.WebPages.DisplayModeProvider.<>c__DisplayClass6.b__5(IDisplayMode mode) at System.Linq.Enumerable.WhereListIterator1.MoveNext() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at System.Web.WebPages.DisplayModeProvider.GetAvailableDisplayModesForContext(HttpContextBase httpContext, IDisplayMode currentDisplayMode) at System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext, String[] locations, String[] areaLocations, String locationsPropertyName, String name, String controllerName, String cacheKeyPrefix, Boolean useCache, String[]& searchedLocations)

It seems I cant get in there and mock the DisplayModeProvider. Per the MVC source Code Does anyone have a solution to this? I havent been able to find a solution to this anywhere.

like image 543
Etch Avatar asked Nov 12 '12 17:11

Etch


1 Answers

First, init mocks with MockBehavior.Strict like:

var context = new Mock<HttpContextBase>(MockBehavior.Strict);

Using this you can find dependency with DisplayMode.

Second, DisplayMode is new feature in asp.net mvc 4:

http://www.asp.net/whitepapers/mvc4-release-notes#_Toc303253810

So, it get Request.Browser params for determinate View which use. Add Mock for browser:

var browser = new Mock<HttpBrowserCapabilitiesBase>(MockBehavior.Strict);
var context = new Mock<HttpContextBase>(MockBehavior.Strict);
var request = new Mock<HttpRequestBase>(MockBehavior.Strict);
var response = new Mock<HttpResponseBase>(MockBehavior.Strict);
var session = new Mock<HttpSessionStateBase>(MockBehavior.Strict);
var server = new Mock<HttpServerUtilityBase>(MockBehavior.Strict);
var cookies = new HttpCookieCollection();
var items = new ListDictionary();

browser.Setup(b => b.IsMobileDevice).Returns(false);

request.Setup(r => r.Cookies).Returns(cookies);
request.Setup(r => r.ValidateInput());
request.Setup(r => r.UserAgent).Returns("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11");
response.Setup(r => r.Cookies).Returns(cookies);

request.Setup(r => r.Browser).Returns(browser.Object);
context.Setup(ctx => ctx.Items).Returns(items);
like image 92
Chernikov Avatar answered Sep 29 '22 05:09

Chernikov