public void Details_Test()
        {
            CC.Data.Tests.Helper.PrepareTestData();
            ClientsController target = new ClientsController(); // TODO: Initialize to an appropriate value
            User ccUser = CC.Data.Tests.Helper.GetAdminUser();
            var  mocks  = new ContextMocks(target, true, false);

            target.CcUser = ccUser;
            Client          cc        = CC.Data.Tests.Helper.GetClient("Client1");
            Nullable <int>  id        = cc.Id;
            Nullable <bool> newClient = false;

            ViewResult actual = target.Details(id, newClient);

            Assert.IsNotNull(actual, " view result can not be null");

            System.Web.Mvc.ViewResultBase viewResult = (System.Web.Mvc.ViewResultBase)actual;

            ViewDataDictionary data = viewResult.ViewData;

            Assert.IsNotNull(data.Keys.Count() == 3, "must be 3 dataitems: client data, agencies, permissions");

            Client cl = (Client)data.Model;

            Assert.IsTrue(cl.Id == cc.Id && cl.FirstName == "Client1", " must open detail of client with name " + cc.FirstName);

            //now write the same test for user, that not have permissions for this client"
        }
예제 #2
0
        public static string Render(this ControllerContext context, ViewResultBase viewResult)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (string.IsNullOrEmpty(viewResult.ViewName))
            {
                viewResult.ViewName = context.RouteData.GetRequiredString("action");
            }

            ViewEngineResult viewEngineResult = null;

            if (viewResult.View == null)
            {
                viewEngineResult = (viewResult is PartialViewResult) ?
                                   ViewEngines.Engines.FindPartialView(context, viewResult.ViewName) :
                                   ViewEngines.Engines.FindView(context, viewResult.ViewName, (viewResult as ViewResult).MasterName);
                viewResult.View = viewEngineResult.View;
            }
            try
            {
                var output      = new StringWriter(CultureInfo.InvariantCulture);
                var viewContext = new ViewContext(context, viewResult.View, viewResult.ViewData, viewResult.TempData, output);
                viewResult.View.Render(viewContext, output);
                return(output.ToString());
            }
            finally
            {
                if (viewEngineResult != null)
                {
                    viewEngineResult.ViewEngine.ReleaseView(context, viewResult.View);
                }
            }
        }
 public AutoMapViewResult(Type sourceType, Type destinationType, object domainModel, ViewResultBase view)
 {
     SourceType = sourceType;
     DestinationType = destinationType;
     DomainModel = domainModel;
     View = view;
 }
예제 #4
0
파일: ViewTool.cs 프로젝트: BlueInt32/Tools
        public static string RenderPartialViewToString(ControllerContext context, ViewResultBase partialViewResult)
        {
            using (var sw = new StringWriter())
            {
                if (string.IsNullOrEmpty(partialViewResult.ViewName))
                {
                    partialViewResult.ViewName = context.RouteData.GetRequiredString("action");
                }
                ViewEngineResult result = null;
                if (partialViewResult.View == null)
                {
                    result = partialViewResult.ViewEngineCollection.FindPartialView(context, partialViewResult.ViewName);
                    if (result.View == null)
                        throw new InvalidOperationException(
                                       "Unable to find view. Searched in: " +
                                       string.Join(",", result.SearchedLocations));
                    partialViewResult.View = result.View;
                }

                var view = partialViewResult.View;
                var viewContext = new ViewContext(context, view, partialViewResult.ViewData,
                                                  partialViewResult.TempData, sw);
                view.Render(viewContext, sw);
                if (result != null)
                {
                    result.ViewEngine.ReleaseView(context, view);
                }
                return sw.ToString();
            }
        }
 private void SetMenu(ResultExecutingContext filterContext, ViewResultBase viewResult)
 {
     var menu = Menu.Begin();
     menu.SetListClass("sf-menu");
     _Services.ForEach(service => menu.Add(BuildSection(service)));
     menu.Prepare(filterContext.Controller.ControllerContext);
     viewResult.ViewData[MenuKey] = menu.RenderHtml();
 }
        private static void AssertUserEditViewDataIsCorrect(ViewResultBase result)
        {
            var viewData = result.ViewData.Model as ShopViewData;
            Assert.IsNotNull(viewData, "ViewData is not ShopViewData");

            // there should be some roles
            Assert.IsNotNull(viewData.Roles, "viewData.Roles is null");

            // add should have inserted a default user into the viewData
            Assert.IsNotNull(viewData.User, "viewData User is null");
        }
예제 #7
0
 public static void TestHasSymNetInfo(ViewResultBase actual)
 {
     if (actual != null)
     {
         Assert.IsNotNull(actual.ViewBag.SymNetInfo);
     }
     else
     {
         Assert.Fail("Test fail in the call of TestHasSymNetInfo : actual argument is null");
     }
 }
        /// <summary>
        /// Returns the string output from a ViewResultBase object
        /// </summary>
        /// <param name="controllerContext"></param>
        /// <param name="viewResult"></param>
        /// <returns></returns>
        public static string RenderViewResultAsString(this ControllerContext controllerContext, ViewResultBase viewResult)
        {
            using (var sw = new StringWriter())
            {
                controllerContext.EnsureViewObjectDataOnResult(viewResult);

                var viewContext = new ViewContext(controllerContext, viewResult.View, viewResult.ViewData, viewResult.TempData, sw);
                viewResult.View.Render(viewContext, sw);
                foreach(var v in viewResult.ViewEngineCollection)
                {
                    v.ReleaseView(controllerContext, viewResult.View);
                }                
                return sw.ToString().Trim();
            }
        }
 private static void AssertEditViewIsCorrectlyCalled(ViewResultBase result)
 {
     Assert.AreEqual("Edit", result.ViewName);
     var viewData = result.ViewData.Model as ShopViewData;
     Assert.IsNotNull(viewData, "viewData is not ShopViewData");
     Assert.IsNotNull(viewData.Product, "viewData.Product should not be null");
     Assert.IsNotNull(viewData.Categories, "viewData.Categories should not be null");
 }
예제 #10
0
 public static string Render(this Controller controller, ViewResultBase viewResult)
 {
     return(Render(controller.ControllerContext, viewResult));
 }
예제 #11
0
 public ViewResultTest(ViewResultBase viewResult, Controller controller)
     : base(viewResult, controller)
 {
 }
 private static void AssertEditViewIsCorrectlyCalled(ViewResultBase result)
 {
     Assert.AreEqual("Edit", result.ViewName);
     var viewData = result.ViewData.Model as ProductViewData;
     Assert.IsNotNull(viewData, "viewData is not ShopViewData");
 }
예제 #13
0
 private static void VerifyViewResult(ViewResultBase viewResultBase, string viewName, object model)
 {
     Assert.Equal(viewName ?? String.Empty, viewResultBase.ViewName);
     Assert.Equal(model, viewResultBase.Model, new PropertyEqualityComparer());
 }
 private ActionResult FormatViewResult(ViewResultBase view)
 {
     switch (RequestedFormat)
     {
         case FileFormat.Html:
             return view;
         case FileFormat.Json:
             return new JsonResult{ Data = view.ViewData.Model, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
         case FileFormat.Xml:
             return new XmlResult(view.ViewData.Model);
         default:
             //throw new FormatException(string.Concat("Cannot server the content in the request format: ", RequestedFormat));
             return view;
     }
 }
 public GridViewResultAdapterTests()
 {
     viewResult = new ViewResult();
 }
예제 #16
0
 public DebugModelResult(ViewResultBase viewResultBase) {
     _viewResultBase = viewResultBase;
 }
예제 #17
0
 public GridViewResultAdapter(ViewResultBase viewResult)
 {
     var viewData = viewResult.ViewData;
     modelState = viewData.ModelState;
     model = viewData.Model as IGridModel ?? new GridModel();
 }
예제 #18
0
 public ViewResultWrapper(ViewResultBase viewResult)
 {
     Contract.Requires(viewResult != null);
     _viewResult = viewResult;
     _model = viewResult.ViewData.Model;
 }
 public HeaderContributingViewResultAdapter(ViewResultBase innerResult)
 {
     Arg.NotNull("innerResult", innerResult);
     InnerResult = innerResult;
     HeaderViewNameFormat = "{0}.header";
 }
        public virtual bool TryGetResult(ViewResultBase viewResult, ContentType responseFormat, out MultiFormatActionResult actionResult)
        {
            if (FormatManager.Current.CanSerialize(responseFormat))
            {
                if (viewResult.ViewData.Model == null)
                {
                    throw new HttpException((int)this.StatusOnNullModel, this.StatusOnNullModel.ToString());
                }
                actionResult = new MultiFormatActionResult(viewResult.ViewData.Model, responseFormat);
                return true;
            }

            switch (responseFormat.MediaType)
            {
                case "application/octet-stream":
                case "application/x-www-form-urlencoded":
                case "text/html":
                case "*/*":
                    actionResult = null;
                    return true;
                default:
                    actionResult = null;
                    return false;
            }
        }
        /// <summary>
        /// Normally in MVC the way that the View object gets assigned to the result is to Execute the ViewResult, this however
        /// will write to the Response output stream which isn't what we want. Instead, this method will use the same logic inside
        /// of MVC to assign the View object to the result but without executing it. This also ensures that the ViewData and the TempData
        /// is assigned from the controller.
        /// This is only relavent for view results of PartialViewResult or ViewResult.
        /// </summary>
        /// <param name="result"></param>
        /// <param name="controllerContext"></param>
        internal static void EnsureViewObjectDataOnResult(this ControllerContext controllerContext, ViewResultBase result)
        {
            result.ViewData.ModelState.Merge(controllerContext.Controller.ViewData.ModelState);

            // Temporarily copy the dictionary to avoid enumerator-modification errors
            var newViewDataDict = new ViewDataDictionary(controllerContext.Controller.ViewData);
            foreach (var d in newViewDataDict)
                result.ViewData[d.Key] = d.Value;    

            result.TempData = controllerContext.Controller.TempData;

            if (result.View != null) return;

            if (string.IsNullOrEmpty(result.ViewName))
                result.ViewName = controllerContext.RouteData.GetRequiredString("action");

            if (result.View != null) return;

            if (result is PartialViewResult)
            {
                var viewEngineResult = ViewEngines.Engines.FindPartialView(controllerContext, result.ViewName);

                if (viewEngineResult.View == null)
                {
                    throw new InvalidOperationException("Could not find the view " + result.ViewName + ", the following locations were searched: " + Environment.NewLine + string.Join(Environment.NewLine, viewEngineResult.SearchedLocations));
                }
                
                result.View = viewEngineResult.View;
            }
            else if (result is ViewResult)
            {
                var vr = (ViewResult) result;
                var viewEngineResult = ViewEngines.Engines.FindView(controllerContext, vr.ViewName, vr.MasterName);

                if (viewEngineResult.View == null)
                {
                    throw new InvalidOperationException("Could not find the view " + vr.ViewName + ", the following locations were searched: " + Environment.NewLine + string.Join(Environment.NewLine, viewEngineResult.SearchedLocations));
                }

                result.View = viewEngineResult.View;
            }
        }
예제 #22
0
 private void AssertCountOfResourceList(int count, ViewResultBase viewResult)
 {
     Assert.Equal(count, (viewResult.ViewData.Model as IList<ResourceListViewModel>).Count);
 }
 private static string GetViewName(ActionExecutedContext filterContext, ViewResultBase viewResultBase)
 {
     var viewName = viewResultBase.ViewName;
     if (string.IsNullOrEmpty(viewName)) viewName = filterContext.RouteData.GetRequiredString("action");
     return viewName;
 }