10

我目前正在使用Scott Hanselmans HTTP 上下文模拟进行单元测试。这对 MVC 3 来说效果很好,而且从不回头,我用它来测试对以下代码的调用。

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

当我第一次将我的应用程序转换为 MVC 4 时,我遇到了问题,它出现了运行时异常。因此,我尝试通过并修复需要修复的内容,这使我改变了 Hanselmans MockHelpers 上的以下方法:(我基本上更改了 HttpContext.Items 以获取返回,因为它吹出了“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;
    }

这是一个非常简单的 nUnit 测试,我必须尝试确定我需要对这个 http 上下文模拟进行哪些更改(我什至还没有放入断言)

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

现在,当我运行此测试时,它会在 PartialViewRenderer.Render 在线调用上炸毁ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName); 这是以下堆栈跟踪。

在 System.Web.WebPages.DisplayModeProvider.<.ctor>b__2(HttpContextBase context) 在 System.Web.WebPages.DefaultDisplayMode.CanHandleContext(HttpContextBase httpContext) 在 System.Web.WebPages.DisplayModeProvider.<>c__DisplayClass6.b__5(IDisplayMode 模式)在 System.Linq.Enumerable.WhereListIterator 1.MoveNext() at System.Collections.Generic.List1..ctor(IEnumerable 1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 源) 在 System.Web.WebPages.DisplayModeProvider.GetAvailableDisplayModesForContext(HttpContextBase httpContext, IDisplayMode currentDisplayMode) 在 System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext, String[ ] 位置,字符串 [] areaLocations,字符串位置属性名称,字符串名称,字符串控制器名称,字符串 cacheKeyPrefix,布尔 useCache,字符串 []& 搜索位置)

看来我无法进入那里并模拟 DisplayModeProvider。 根据 MVC 源代码有没有人对此有解决方案?我无法在任何地方找到解决方案。

4

2 回答 2

6

首先,init 模拟MockBehavior.Strict如下:

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

使用它,您可以找到与 DisplayMode 的依赖关系。

其次,DisplayMode 是 asp.net mvc 4 中的新特性:

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

因此,它获取Request.Browser用于确定 View 的参数。为浏览器添加 Mock:

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);
于 2012-11-23T12:14:40.573 回答
0

DisplayMode implements the interface IDisplayMode, so you should be able to (with Moq) create a mock to inject. DisplayModeProvider has a SetDisplayMode (protected) method you may be able to invoke via Reflection.

于 2012-11-12T17:58:45.633 回答