public static HtmlHelper <TModel> GetHtmlHelper <TModel>( TModel model, ControllerBase controller, bool clientValidationEnabled) { ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(new FakeViewEngine()); var httpContext = new FakeHttpContext(); var viewData = new FakeViewDataContainer { ViewData = new ViewDataDictionary <TModel>(model) }; var routeData = new RouteData(); routeData.Values["controller"] = "home"; routeData.Values["action"] = "index"; ControllerContext controllerContext = new FakeControllerContext(controller); var viewContext = new FakeViewContext(controllerContext, "MyView", routeData); viewContext.HttpContext = httpContext; viewContext.ClientValidationEnabled = clientValidationEnabled; viewContext.UnobtrusiveJavaScriptEnabled = clientValidationEnabled; viewContext.FormContext = new FakeFormContext(); HtmlHelper <TModel> htmlHelper = new HtmlHelper <TModel>(viewContext, viewData); return(htmlHelper); }
public static ViewContext MockViewContext(ViewDataDictionary viewDataDictionary) { var httpContext = FakeHttpContext(); var mockViewContext = new FakeViewContext( new ControllerContext( httpContext, new RouteData(), new Mock <ControllerBase>().Object), new Mock <IView>().Object, viewDataDictionary, new TempDataDictionary(), new StringWriter()); return(mockViewContext); }