在单元测试中将MVC2视图渲染为字符串

时间:2014-11-20 11:25:19

标签: c# asp.net-mvc-2 moq

我正在尝试将视图渲染到单元测试中的字符串(因此没有HostingEnvironment,没有VirtualPathProvider)然后断言正则表达式结果以添加特定值的线束以便不进行更改。 我的渲染方法(很多都是在这个网站上找到的样本):

  private string RenderHomeIndexAction()
    {
      var controller = new HomeController();
      using (var writer = new StringWriter())
      {
        var httpContext = new HttpContext(new HttpRequest(string.Empty, "http://example.com", string.Empty), new HttpResponse(writer));
        HttpContext.Current = httpContext;
        var controllerName = controller.GetType().Name;
        var routeData = new RouteData();
        routeData.Values.Add("controller", typeofHomeController("Controller", string.Empty));
        routeData.Values.Add("action", "index");
        var controllerContext = new ControllerContext(new HttpContextWrapper(httpContext), routeData, controller);
        controller.ControllerContext = controllerContext;
        var res = controller.Index();
        res.ExecuteResult(controllerContext);
        HttpContext.Current = null;
        return writer.ToString();
      }
    }

这将使用以下stactkrace在res.ExecuteResult(controllerContext)中抛出NullReferenceException:

   at System.Web.VirtualPath.GetCacheKey()
   at System.Web.Compilation.BuildManager.GetCacheKeyFromVirtualPath(VirtualPath virtualPath, Boolean& keyFromVPP)
   at System.Web.Compilation.BuildManager.GetVPathBuildResultFromCacheInternal(VirtualPath virtualPath, Boolean ensureIsUpToDate)
   at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
   at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
   at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound)
   at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp)
   at System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp)
   at System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(String virtualPath, Type requiredBaseType)
   at System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.CreateInstanceFromVirtualPath(String virtualPath, Type requiredBaseType)
   at System.Web.Mvc.WebFormViewEngine.FileExists(ControllerContext controllerContext, String virtualPath)
   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 System.Web.Mvc.ViewEngineCollection.<>c__DisplayClass6.<FindView>b__5(IViewEngine e)
   at System.Web.Mvc.ViewEngineCollection.Find(Func`2 cacheLocator, Func`2 locator)
   at System.Web.Mvc.ViewEngineCollection.FindView(ControllerContext controllerContext, String viewName, String masterName)
   at System.Web.Mvc.ViewResult.FindView(ControllerContext context)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)

0 个答案:

没有答案