我想單元測試一個具有controllercontext參數的無效方法以及一些發送電子郵件的方法。電子郵件模板使用視圖引擎渲染部分視圖我嘲笑controllercontext。所以渲染視圖拋出一個異常,因爲ControllerContext具有空值。'System.Web.Mvc.ViewEngines'的類型初始值設定項引發了一個異常
public static string RenderViewToString(ControllerContext context, string viewPath, object model, bool partial = false)
{
var viewEngineResult = partial ? ViewEngines.Engines.FindPartialView(context, viewPath) : ViewEngines.Engines.FindView(context, viewPath, null);
if (viewEngineResult == null)
throw new FileNotFoundException("View cannot be found.");
var view = viewEngineResult.View;
context.Controller.ViewData.Model = model;
var result = String.Empty;
using (var sw = new StringWriter())
{
var ctx = new ViewContext(context, view,
context.Controller.ViewData,
context.Controller.TempData,
sw);
view.Render(ctx, sw);
result = sw.ToString();
}
return result;
}
請張貼一些代碼 – NicoRiff
沒有人可以幫助你,如果你不提供你的代碼。 – Sefe
@NicoRiff有我的代碼 –