我目前正在使用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.List
1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable
1 源) 在 System.Web.WebPages.DisplayModeProvider.GetAvailableDisplayModesForContext(HttpContextBase httpContext, IDisplayMode currentDisplayMode) 在 System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext, String[ ] 位置,字符串 [] areaLocations,字符串位置属性名称,字符串名称,字符串控制器名称,字符串 cacheKeyPrefix,布尔 useCache,字符串 []& 搜索位置)
看来我无法进入那里并模拟 DisplayModeProvider。 根据 MVC 源代码有没有人对此有解决方案?我无法在任何地方找到解决方案。