I am trying to unit test a void method that have controllercontext parameter and some more to send email. Email template render partial view using view engine. I mocked controllercontext. So render view throw an exception because ControllerContext have null value.
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;
}
I dont think the error is with your code. I think it is with you test project does not have all the dependencies, in package mananger, with the test project set at the Default project intal
PM > Install-Package Microsoft.AspNet.WebPages
I forget to add this everytime I do test on a MVC project.