Esempio n. 1
0
        public void FindViewDelegatesToResolver()
        {
            // Arrange
            Mock <IView>       view          = new Mock <IView>();
            ControllerContext  context       = new ControllerContext();
            Mock <IViewEngine> locatedEngine = new Mock <IViewEngine>();
            ViewEngineResult   engineResult  = new ViewEngineResult(view.Object, locatedEngine.Object);

            locatedEngine.Setup(e => e.FindView(context, "ViewName", "MasterName", true))
            .Returns(engineResult);
            Mock <IViewEngine> secondEngine = new Mock <IViewEngine>();

            var resolver = new Mock <IDependencyResolver>();

            resolver.Setup(r => r.GetServices(typeof(IViewEngine))).Returns(new IViewEngine[] { locatedEngine.Object, secondEngine.Object });

            ViewEngineCollection engines = new ViewEngineCollection(new IViewEngine[0], resolver.Object);

            // Act
            ViewEngineResult result = engines.FindView(context, "ViewName", "MasterName");

            // Assert
            Assert.Same(engineResult, result);
            secondEngine.Verify(e => e.FindView(context, "ViewName", "MasterName", It.IsAny <bool>()), Times.Never());
        }
Esempio n. 2
0
        public void FindViewIteratesThroughCollectionUntilFindsSuccessfulEngine()
        {
            // Arrange
            ControllerContext    context       = new Mock <ControllerContext>().Object;
            ViewEngineCollection collection    = new ViewEngineCollection();
            Mock <IViewEngine>   engine1       = new Mock <IViewEngine>();
            ViewEngineResult     engine1Result = new ViewEngineResult(
                new[] { "location1", "location2" }
                );

            engine1
            .Setup(e => e.FindView(context, "view", "master", It.IsAny <bool>()))
            .Returns(engine1Result);
            Mock <IViewEngine> engine2       = new Mock <IViewEngine>();
            ViewEngineResult   engine2Result = new ViewEngineResult(
                new Mock <IView>().Object,
                engine2.Object
                );

            engine2
            .Setup(e => e.FindView(context, "view", "master", It.IsAny <bool>()))
            .Returns(engine2Result);
            collection.Add(engine1.Object);
            collection.Add(engine2.Object);

            // Act
            ViewEngineResult result = collection.FindView(context, "view", "master");

            // Assert
            Assert.Same(engine2Result, result);
        }
        public void AsksEachViewEngineInTheOrderItWasCalled()
        {
            var redEngine = new Mock <IViewEngine>();
            var redResult = new ViewEngineResult(false, new[] { "Whatever" });

            redEngine.Setup(x => x.FindView(It.IsAny <ControllerContext>(), It.IsAny <ViewResultOptions>(), It.IsAny <string>())).Returns(redResult);

            var orangeEngine = new Mock <IViewEngine>();

            orangeEngine.Setup(x => x.FindView(It.IsAny <ControllerContext>(), It.IsAny <ViewResultOptions>(), It.IsAny <string>())).Returns((ViewEngineResult)null);

            var blueEngine = new Mock <IViewEngine>();
            var blueResult = new ViewEngineResult(true, new string[0]);

            blueEngine.Setup(x => x.FindView(It.IsAny <ControllerContext>(), It.IsAny <ViewResultOptions>(), It.IsAny <string>())).Returns(blueResult);

            var greenEngine = new Mock <IViewEngine>();
            var greenResult = new ViewEngineResult(true, new string[0]);

            greenEngine.Setup(x => x.FindView(It.IsAny <ControllerContext>(), It.IsAny <ViewResultOptions>(), It.IsAny <string>())).Returns(greenResult);

            var collection = new ViewEngineCollection();

            collection.Add(redEngine.Object);
            collection.Add(orangeEngine.Object);
            collection.Add(blueEngine.Object);
            collection.Add(greenEngine.Object);
            var result = collection.FindView(RequestBuilder.CreateRequest().BuildControllerContext(), new ViewResultOptions(), "Foo");

            Assert.AreEqual(blueResult, result);
        }
Esempio n. 4
0
        public void FindViewRemovesDuplicateSearchedLocationsFromMultipleEngines()
        {
            // Arrange
            ControllerContext  context       = new Mock <ControllerContext>().Object;
            Mock <IViewEngine> engine1       = new Mock <IViewEngine>();
            ViewEngineResult   engine1Result = new ViewEngineResult(new[] { "repeatLocation", "location1" });

            engine1.Setup(e => e.FindView(context, "view", "master", It.IsAny <bool>())).Returns(engine1Result);
            Mock <IViewEngine> engine2       = new Mock <IViewEngine>();
            ViewEngineResult   engine2Result = new ViewEngineResult(new[] { "location2", "repeatLocation" });

            engine2.Setup(e => e.FindView(context, "view", "master", It.IsAny <bool>())).Returns(engine2Result);
            ViewEngineCollection collection = new ViewEngineCollection()
            {
                engine1.Object,
                engine2.Object,
            };

            // Act
            ViewEngineResult result = collection.FindView(context, "view", "master");

            // Assert
            Assert.Null(result.View);
            var expectedLocations = new[] { "repeatLocation", "location1", "location2" };

            Assert.Equal(expectedLocations, result.SearchedLocations.ToArray());
        }
Esempio n. 5
0
        protected bool FindView(string viewName, object model, out ActionResult action)
        {
            var layoutAwareViewEngine = WorkContext.Resolve <ILayoutAwareViewEngine>();
            var engines = new ViewEngineCollection(new List <IViewEngine> {
                layoutAwareViewEngine
            });
            var result = engines.FindView(ControllerContext, viewName, null);

            if (result.View != null)
            {
                if (model != null)
                {
                    ViewData.Model = model;
                }

                action = new ViewResult
                {
                    ViewData = ViewData,
                    TempData = TempData,
                    View     = result.View,
                    ViewName = viewName
                };
                return(true);
            }
            action = null;
            return(false);
        }
Esempio n. 6
0
        public void ExecuteResult(ControllerContext context, TextWriter writer)
        {
            Requires.NotNull("context", context);
            Requires.NotNull("writer", writer);

            if (String.IsNullOrEmpty(ViewName))
            {
                ViewName = context.RouteData.GetRequiredString("action");
            }

            ViewEngineResult result = null;

            if (View == null)
            {
                result = ViewEngineCollection.FindView(context, ViewName, MasterName);
                View   = result.View;
            }

            var viewContext = new ViewContext(context, View, ViewData, TempData, writer);

            View.Render(viewContext, writer);

            if (result != null)
            {
                result.ViewEngine.ReleaseView(context, View);
            }
        }
Esempio n. 7
0
        public void FindViewIgnoresSearchLocationsFromCache()
        {
            // Arrange
            ControllerContext  context = new Mock <ControllerContext>().Object;
            Mock <IViewEngine> engine  = new Mock <IViewEngine>();

            engine
            .Setup(e => e.FindView(context, "view", "master", true))
            .Returns(new ViewEngineResult(new[] { "cachePath" }));
            engine
            .Setup(e => e.FindView(context, "view", "master", false))
            .Returns(new ViewEngineResult(new[] { "locatorPath" }));
            ViewEngineCollection collection = new ViewEngineCollection()
            {
                engine.Object,
            };

            // Act
            ViewEngineResult result = collection.FindView(context, "view", "master");

            // Assert
            string searchedLocation = Assert.Single(result.SearchedLocations);

            Assert.Equal("locatorPath", searchedLocation);
            engine.Verify(e => e.FindView(context, "view", "master", true), Times.Once());
            engine.Verify(e => e.FindView(context, "view", "master", false), Times.Once());
        }
        public void FindViewAggregatesAllSearchedLocationsIfAllEnginesFail()
        {
            // Arrange
            ControllerContext    context       = new Mock <ControllerContext>().Object;
            ViewEngineCollection collection    = new ViewEngineCollection();
            Mock <IViewEngine>   engine1       = new Mock <IViewEngine>();
            ViewEngineResult     engine1Result = new ViewEngineResult(new[] { "location1", "location2" });

            engine1.Expect(e => e.FindView(context, "view", "master", It.IsAny <bool>())).Returns(engine1Result);
            Mock <IViewEngine> engine2       = new Mock <IViewEngine>();
            ViewEngineResult   engine2Result = new ViewEngineResult(new[] { "location3", "location4" });

            engine2.Expect(e => e.FindView(context, "view", "master", It.IsAny <bool>())).Returns(engine2Result);
            collection.Add(engine1.Object);
            collection.Add(engine2.Object);

            // Act
            ViewEngineResult result = collection.FindView(context, "view", "master");

            // Assert
            Assert.IsNull(result.View);
            Assert.AreEqual(4, result.SearchedLocations.Count());
            Assert.IsTrue(result.SearchedLocations.Contains("location1"));
            Assert.IsTrue(result.SearchedLocations.Contains("location2"));
            Assert.IsTrue(result.SearchedLocations.Contains("location3"));
            Assert.IsTrue(result.SearchedLocations.Contains("location4"));
        }
Esempio n. 9
0
        public void FindViewReturnsValueFromFirstSuccessfulEngine()
        {
            // Arrange
            ControllerContext    context       = new Mock <ControllerContext>().Object;
            ViewEngineCollection collection    = new ViewEngineCollection();
            Mock <IViewEngine>   engine1       = new Mock <IViewEngine>();
            ViewEngineResult     engine1Result = new ViewEngineResult(
                new Mock <IView>().Object,
                engine1.Object
                );

            engine1
            .Setup(e => e.FindView(context, "view", "master", It.IsAny <bool>()))
            .Returns(engine1Result);
            Mock <IViewEngine> engine2       = new Mock <IViewEngine>();
            ViewEngineResult   engine2Result = new ViewEngineResult(
                new Mock <IView>().Object,
                engine2.Object
                );

            engine2
            .Setup(e => e.FindView(context, "view", "master", It.IsAny <bool>()))
            .Returns(engine2Result);
            collection.Add(engine1.Object);
            collection.Add(engine2.Object);

            // Act
            ViewEngineResult result = collection.FindView(context, "view", "master");

            // Assert
            Assert.Same(engine1Result, result);
        }
Esempio n. 10
0
        public void FindViewLooksAtLocatorIfCacheEmpty()
        {
            // Arrange
            ControllerContext  context      = new Mock <ControllerContext>().Object;
            Mock <IViewEngine> engine       = new Mock <IViewEngine>();
            ViewEngineResult   engineResult = new ViewEngineResult(
                new Mock <IView>().Object,
                engine.Object
                );

            engine
            .Setup(e => e.FindView(context, "view", "master", true))
            .Returns(new ViewEngineResult(new[] { "path" }));
            engine.Setup(e => e.FindView(context, "view", "master", false)).Returns(engineResult);
            ViewEngineCollection collection = new ViewEngineCollection()
            {
                engine.Object,
            };

            // Act
            ViewEngineResult result = collection.FindView(context, "view", "master");

            // Assert
            Assert.Same(engineResult, result);
            engine.Verify(e => e.FindView(context, "view", "master", true), Times.Once());
            engine.Verify(e => e.FindView(context, "view", "master", false), Times.Once());
        }
        public void AsksEachViewEngineInTheOrderItWasCalled()
        {
            var redEngine = new Mock<IViewEngine>();
            var redResult = new ViewEngineResult(false, new[] { "Whatever"});
            redEngine.Setup(x => x.FindView(It.IsAny<ControllerContext>(), It.IsAny<ViewResultOptions>(), It.IsAny<string>())).Returns(redResult);

            var orangeEngine = new Mock<IViewEngine>();
            orangeEngine.Setup(x => x.FindView(It.IsAny<ControllerContext>(), It.IsAny<ViewResultOptions>(), It.IsAny<string>())).Returns((ViewEngineResult)null);

            var blueEngine = new Mock<IViewEngine>();
            var blueResult = new ViewEngineResult(true, new string[0]);
            blueEngine.Setup(x => x.FindView(It.IsAny<ControllerContext>(), It.IsAny<ViewResultOptions>(), It.IsAny<string>())).Returns(blueResult);

            var greenEngine = new Mock<IViewEngine>();
            var greenResult = new ViewEngineResult(true, new string[0]);
            greenEngine.Setup(x => x.FindView(It.IsAny<ControllerContext>(), It.IsAny<ViewResultOptions>(), It.IsAny<string>())).Returns(greenResult);

            var collection = new ViewEngineCollection();
            collection.Add(redEngine.Object);
            collection.Add(orangeEngine.Object);
            collection.Add(blueEngine.Object);
            collection.Add(greenEngine.Object);
            var result = collection.FindView(RequestBuilder.CreateRequest().BuildControllerContext(), new ViewResultOptions(), "Foo");
            Assert.AreEqual(blueResult, result);
        }
        public void MustRegisterViewEnginesBeforeFindingViews()
        {
            var collection = new ViewEngineCollection();
            var context    = RequestBuilder.CreateRequest().BuildControllerContext();
            var parameters = new ViewResultOptions();

            Assert.Throws <NavigationConfigurationException>(() => collection.FindView(context, parameters, "Foo"));
        }
        public void MustRegisterViewEnginesBeforeFindingViews()
        {
            var collection = new ViewEngineCollection();
            var context = RequestBuilder.CreateRequest().BuildControllerContext();
            var parameters = new ViewResultOptions();

            Assert.Throws<NavigationConfigurationException>(() => collection.FindView(context, parameters, "Foo"));
        }
Esempio n. 14
0
        protected override ViewEngineResult FindView(ControllerContext context)
        {
            ViewEngineResult result = ViewEngineCollection.FindView(context, ViewName, string.Empty);

            if (result.View != null)
            {
                return(result);
            }
            return(null);
        }
        public void FindViewThrowsIfControllerContextIsNull()
        {
            // Arrange
            ViewEngineCollection collection = new ViewEngineCollection();

            // Act & Assert
            ExceptionHelper.ExpectArgumentNullException(
                () => collection.FindView(null, "view", null),
                "controllerContext"
                );
        }
Esempio n. 16
0
        protected override ViewEngineResult FindView(ControllerContext controllerContext)
        {
            var result = ViewEngineCollection.FindView(controllerContext, "RoboFormResult_", null);

            var workContext    = EngineContext.Current.Resolve <IWebWorkContext>();
            var scriptRegister = new ScriptRegister(workContext);
            var styleRegister  = new StyleRegister(workContext);

            GetAdditionalResources(scriptRegister, styleRegister);

            return(result);
        }
Esempio n. 17
0
        public void FindViewThrowsIfViewNameIsEmpty()
        {
            // Arrange
            ControllerContext    context    = new Mock <ControllerContext>().Object;
            ViewEngineCollection collection = new ViewEngineCollection();

            // Act & Assert
            Assert.ThrowsArgumentNullOrEmpty(
                () => collection.FindView(context, "", null),
                "viewName"
                );
        }
        public void FindViewThrowsIfViewNameIsNull()
        {
            // Arrange
            ControllerContext    context    = new Mock <ControllerContext>().Object;
            ViewEngineCollection collection = new ViewEngineCollection();

            // Act & Assert
            ExceptionHelper.ExpectArgumentExceptionNullOrEmpty(
                () => collection.FindView(context, null, null),
                "viewName"
                );
        }
        public void FindViewReturnsNoViewAndEmptySearchedLocationsIfCollectionEmpty()
        {
            // Arrange
            ControllerContext    context    = new Mock <ControllerContext>().Object;
            ViewEngineCollection collection = new ViewEngineCollection();

            // Act
            ViewEngineResult result = collection.FindView(context, "view", null);

            // Assert
            Assert.IsNull(result.View);
            Assert.AreEqual(0, result.SearchedLocations.Count());
        }
Esempio n. 20
0
        IView CreateView(string viewName, ControllerContext controllerContext)
        {
            var result = viewEngines.FindView(controllerContext, viewName, null);

            if (result.View != null)
            {
                return(result.View);
            }

            throw new Exception(
                      "Email view not found for " + viewName +
                      ". Locations searched:" + Environment.NewLine +
                      string.Join(Environment.NewLine, result.SearchedLocations)
                      );
        }
        public ActionResult Error()
        {
            var model = new Models.ErrorModel(ControllerContext);

            if (model.StatusCode != null)
            {
                //try to locate the view for this error
                var result = ViewEngineCollection.FindView(ControllerContext, $"Error{model.StatusCode}", null);
                if (result?.View != null) //if found, execute it, decorating it with our decorator which preserves the original status code.
                {
                    return(Decorate(View(result.View, model), model));
                }
            }

            return(Decorate(View(model), model));
        }
        public static bool ViewExists(
            this ViewEngineCollection viewEngines,
            ControllerContext controllerContext,
            string viewName, bool isPartial = false)
        {
            var result = !isPartial
                ? viewEngines.FindView(controllerContext, viewName, null)
                : viewEngines.FindPartialView(controllerContext, viewName);

            if (result.View != null)
            {
                return(true);
            }

            return(false);
        }
        public static bool ViewExists(this ViewEngineCollection viewEngines,
                                      ControllerContext controllerContext,
                                      string viewName, bool isPartial = false)
        {
            var result = !isPartial
                ? viewEngines.FindView(controllerContext, viewName, null)
                : viewEngines.FindPartialView(controllerContext, viewName);

            if (result.View != null)
            {
                return(true);
            }

            LogHelper.Warn <Bootstrap>("No view file found with the name " + viewName);
            return(false);
        }
        public void FindViewSuccessWithOneEngine()
        {
            // Arrange
            ControllerContext    context      = new Mock <ControllerContext>().Object;
            ViewEngineCollection collection   = new ViewEngineCollection();
            Mock <IViewEngine>   engine       = new Mock <IViewEngine>();
            ViewEngineResult     engineResult = new ViewEngineResult(new Mock <IView>().Object, engine.Object);

            engine.Expect(e => e.FindView(context, "view", "master", It.IsAny <bool>())).Returns(engineResult);
            collection.Add(engine.Object);

            // Act
            ViewEngineResult result = collection.FindView(context, "view", "master");

            // Assert
            Assert.AreSame(engineResult, result);
        }
        public static bool ViewExists(
            this ViewEngineCollection viewEngines,
            ControllerContext controllerContext,
            string viewName, bool isPartial = false)
        {
            var result = !isPartial
                ? viewEngines.FindView(controllerContext, viewName, null)
                : viewEngines.FindPartialView(controllerContext, viewName);

            if (result.View != null)
            {
                return(true);
            }

            LogHelper.Info <ViewEngineCollection>("[DocTypeGridEditor] No view file found with the name '{0}'", () => viewName);

            return(false);
        }
Esempio n. 26
0
        public void Download()
        {
            using (StringWriter sw = new StringWriter())
            {
                var preview = ViewEngineCollection.FindView(this.ControllerContext, "Report", null);
                ViewData.Add("users", GetUsers());
                var vc = new ViewContext(ControllerContext, preview.View, ViewData, TempData, sw);
                preview.View.Render(vc, sw);

                HtmlToPdf   htp = new HtmlToPdf();
                PdfDocument doc = htp.ConvertHtmlString(sw.ToString());
                using (MemoryStream ms = new MemoryStream())
                {
                    doc.Save(System.Web.HttpContext.Current.Response, false, "BPReport.pdf");
                    doc.Close();
                }
            }
        }
        // Enable the call to ViewEngineCollection FindView method with useCache=false
        public static ViewEngineResult FindView(this ViewEngineCollection viewEngineCollection, ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            try
            {
                var cacheKey   = CreateCacheKey(controllerContext, "View", viewName, masterName, (controllerContext.Controller as IDnnController)?.ModuleContext.PortalId ?? 0);
                var parameters = new object[]
                {
                    new Func <IViewEngine, ViewEngineResult>(e => e.FindView(controllerContext, viewName, masterName, false)),
                    false,
                };
                var cacheArg = new CacheItemArgs(cacheKey, 120, CacheItemPriority.Default, "Find", viewEngineCollection, parameters);

                return(useCache ? CBO.GetCachedObject <ViewEngineResult>(cacheArg, CallFind) : CallFind(cacheArg));
            }
            catch (Exception)
            {
                return(viewEngineCollection.FindView(controllerContext, viewName, masterName));
            }
        }
        public void CombinesAllErrorsWhenResultIsNotFound()
        {
            var redEngine = new Mock<IViewEngine>();
            var redResult = new ViewEngineResult(false, new[] { "red whatever" });
            redEngine.Setup(x => x.FindView(It.IsAny<ControllerContext>(), It.IsAny<ViewResultOptions>(), It.IsAny<string>())).Returns(redResult);

            var blueEngine = new Mock<IViewEngine>();
            var blueResult = new ViewEngineResult(false, new[] { "blue whatever" });
            blueEngine.Setup(x => x.FindView(It.IsAny<ControllerContext>(), It.IsAny<ViewResultOptions>(), It.IsAny<string>())).Returns(blueResult);

            var collection = new ViewEngineCollection();
            collection.Add(redEngine.Object);
            collection.Add(blueEngine.Object);
            var result = collection.FindView(RequestBuilder.CreateRequest().BuildControllerContext(), new ViewResultOptions(), "Foo");
            Assert.IsFalse(result.Success);
            Assert.AreEqual(2, result.SearchLocations.Count());
            Assert.AreEqual("red whatever", result.SearchLocations.ElementAt(0));
            Assert.AreEqual("blue whatever", result.SearchLocations.ElementAt(1));
        }
        public void FindViewFailureWithOneEngine()
        {
            // Arrange
            ControllerContext    context      = new Mock <ControllerContext>().Object;
            ViewEngineCollection collection   = new ViewEngineCollection();
            Mock <IViewEngine>   engine       = new Mock <IViewEngine>();
            ViewEngineResult     engineResult = new ViewEngineResult(new[] { "location1", "location2" });

            engine.Expect(e => e.FindView(context, "view", "master", It.IsAny <bool>())).Returns(engineResult);
            collection.Add(engine.Object);

            // Act
            ViewEngineResult result = collection.FindView(context, "view", "master");

            // Assert
            Assert.IsNull(result.View);
            Assert.AreEqual(2, result.SearchedLocations.Count());
            Assert.IsTrue(result.SearchedLocations.Contains("location1"));
            Assert.IsTrue(result.SearchedLocations.Contains("location2"));
        }
Esempio n. 30
0
        protected ViewEngineResult FindView(ControllerContext context)
        {
            ViewEngineResult result = _viewEngines.FindView(context, ViewName, MasterName);

            if (result.View != null)
            {
                return(result);
            }

            // we need to generate an exception containing all the locations we searched
            StringBuilder locationsText = new StringBuilder();

            foreach (string location in result.SearchedLocations)
            {
                locationsText.AppendLine();
                locationsText.Append(location);
            }
            throw new InvalidOperationException(String.Format(CultureInfo.CurrentUICulture,
                                                              MvcResources.Common_ViewNotFound, ViewName, locationsText));
        }
        private static IView FindPartialView(ViewContext viewContext, string partialViewName,
                                             ViewEngineCollection viewEngineCollection, string masterName)
        {
            ViewEngineResult result = viewEngineCollection.FindView(viewContext, partialViewName, masterName);

            if (result.View != null)
            {
                return(result.View);
            }

            var locationsText = new StringBuilder();

            foreach (string location in result.SearchedLocations)
            {
                locationsText.AppendLine();
                locationsText.Append(location);
            }

            throw new InvalidOperationException(String.Format(CultureInfo.CurrentUICulture,
                                                              "could not find view {0} looked in {1}", partialViewName,
                                                              locationsText));
        }
        public void FindViewDelegatesToResolver()
        {
            // Arrange
            Mock <IView>       view          = new Mock <IView>();
            ControllerContext  context       = new ControllerContext();
            Mock <IViewEngine> locatedEngine = new Mock <IViewEngine>();
            ViewEngineResult   engineResult  = new ViewEngineResult(view.Object, locatedEngine.Object);

            locatedEngine.Setup(e => e.FindView(context, "ViewName", "MasterName", true))
            .Returns(engineResult);
            Mock <IViewEngine> secondEngine = new Mock <IViewEngine>();
            Resolver <IEnumerable <IViewEngine> > resolver = new Resolver <IEnumerable <IViewEngine> > {
                Current = new IViewEngine[] { locatedEngine.Object, secondEngine.Object }
            };
            ViewEngineCollection engines = new ViewEngineCollection(resolver);

            // Act
            ViewEngineResult result = engines.FindView(context, "ViewName", "MasterName");

            // Assert
            Assert.Same(engineResult, result);
            secondEngine.Verify(e => e.FindView(context, "ViewName", "MasterName", It.IsAny <bool>()), Times.Never());
        }
        public void CombinesAllErrorsWhenResultIsNotFound()
        {
            var redEngine = new Mock <IViewEngine>();
            var redResult = new ViewEngineResult(false, new[] { "red whatever" });

            redEngine.Setup(x => x.FindView(It.IsAny <ControllerContext>(), It.IsAny <ViewResultOptions>(), It.IsAny <string>())).Returns(redResult);

            var blueEngine = new Mock <IViewEngine>();
            var blueResult = new ViewEngineResult(false, new[] { "blue whatever" });

            blueEngine.Setup(x => x.FindView(It.IsAny <ControllerContext>(), It.IsAny <ViewResultOptions>(), It.IsAny <string>())).Returns(blueResult);

            var collection = new ViewEngineCollection();

            collection.Add(redEngine.Object);
            collection.Add(blueEngine.Object);
            var result = collection.FindView(RequestBuilder.CreateRequest().BuildControllerContext(), new ViewResultOptions(), "Foo");

            Assert.IsFalse(result.Success);
            Assert.AreEqual(2, result.SearchLocations.Count());
            Assert.AreEqual("red whatever", result.SearchLocations.ElementAt(0));
            Assert.AreEqual("blue whatever", result.SearchLocations.ElementAt(1));
        }
        public void FindViewDelegatesToResolver()
        {
            // Arrange
            Mock<IView> view = new Mock<IView>();
            ControllerContext context = new ControllerContext();
            Mock<IViewEngine> locatedEngine = new Mock<IViewEngine>();
            ViewEngineResult engineResult = new ViewEngineResult(view.Object, locatedEngine.Object);
            locatedEngine.Setup(e => e.FindView(context, "ViewName", "MasterName", true))
                .Returns(engineResult);
            Mock<IViewEngine> secondEngine = new Mock<IViewEngine>();

            var resolver = new Mock<IDependencyResolver>();
            resolver.Setup(r => r.GetServices(typeof(IViewEngine))).Returns(new IViewEngine[] { locatedEngine.Object, secondEngine.Object });

            ViewEngineCollection engines = new ViewEngineCollection(new IViewEngine[0], resolver.Object);

            // Act
            ViewEngineResult result = engines.FindView(context, "ViewName", "MasterName");

            // Assert
            Assert.Same(engineResult, result);
            secondEngine.Verify(e => e.FindView(context, "ViewName", "MasterName", It.IsAny<bool>()), Times.Never());
        }
        public void FindViewDelegatesToResolver()
        {
            // Arrange
            Mock<IView> view = new Mock<IView>();
            ControllerContext context = new ControllerContext();
            Mock<IViewEngine> locatedEngine = new Mock<IViewEngine>();
            ViewEngineResult engineResult = new ViewEngineResult(view.Object, locatedEngine.Object);
            locatedEngine.Setup(e => e.FindView(context, "ViewName", "MasterName", true))
                .Returns(engineResult);
            Mock<IViewEngine> secondEngine = new Mock<IViewEngine>();
            Resolver<IEnumerable<IViewEngine>> resolver = new Resolver<IEnumerable<IViewEngine>> { Current = new IViewEngine[] { locatedEngine.Object, secondEngine.Object } };
            ViewEngineCollection engines = new ViewEngineCollection(resolver);

            // Act
            ViewEngineResult result = engines.FindView(context, "ViewName", "MasterName");

            // Assert
            Assert.Same(engineResult, result);
            secondEngine.Verify(e => e.FindView(context, "ViewName", "MasterName", It.IsAny<bool>()), Times.Never());
        }
        public void FindViewFailureWithOneEngine()
        {
            // Arrange
            ControllerContext context = new Mock<ControllerContext>().Object;
            ViewEngineCollection collection = new ViewEngineCollection();
            Mock<IViewEngine> engine = new Mock<IViewEngine>();
            ViewEngineResult engineResult = new ViewEngineResult(new[] { "location1", "location2" });
            engine.Setup(e => e.FindView(context, "view", "master", It.IsAny<bool>())).Returns(engineResult);
            collection.Add(engine.Object);

            // Act
            ViewEngineResult result = collection.FindView(context, "view", "master");

            // Assert
            Assert.Null(result.View);
            Assert.Equal(2, result.SearchedLocations.Count());
            Assert.True(result.SearchedLocations.Contains("location1"));
            Assert.True(result.SearchedLocations.Contains("location2"));
        }
        public void FindViewLooksAtLocatorIfCacheEmpty()
        {
            // Arrange
            ControllerContext context = new Mock<ControllerContext>().Object;
            Mock<IViewEngine> engine = new Mock<IViewEngine>();
            ViewEngineResult engineResult = new ViewEngineResult(new Mock<IView>().Object, engine.Object);
            engine.Setup(e => e.FindView(context, "view", "master", true)).Returns(new ViewEngineResult(new[] { "path" }));
            engine.Setup(e => e.FindView(context, "view", "master", false)).Returns(engineResult);
            ViewEngineCollection collection = new ViewEngineCollection()
            {
                engine.Object,
            };

            // Act
            ViewEngineResult result = collection.FindView(context, "view", "master");

            // Assert
            Assert.Same(engineResult, result);
            engine.Verify(e => e.FindView(context, "view", "master", true), Times.Once());
            engine.Verify(e => e.FindView(context, "view", "master", false), Times.Once());
        }
        public void FindViewIgnoresSearchLocationsFromCache()
        {
            // Arrange
            ControllerContext context = new Mock<ControllerContext>().Object;
            Mock<IViewEngine> engine = new Mock<IViewEngine>();
            engine.Setup(e => e.FindView(context, "view", "master", true)).Returns(new ViewEngineResult(new[] { "cachePath" }));
            engine.Setup(e => e.FindView(context, "view", "master", false)).Returns(new ViewEngineResult(new[] { "locatorPath" }));
            ViewEngineCollection collection = new ViewEngineCollection()
            {
                engine.Object,
            };

            // Act
            ViewEngineResult result = collection.FindView(context, "view", "master");

            // Assert
            string searchedLocation = Assert.Single(result.SearchedLocations);
            Assert.Equal("locatorPath", searchedLocation);
            engine.Verify(e => e.FindView(context, "view", "master", true), Times.Once());
            engine.Verify(e => e.FindView(context, "view", "master", false), Times.Once());
        }
        public void FindViewIteratesThroughCollectionUntilFindsSuccessfulEngine()
        {
            // Arrange
            ControllerContext context = new Mock<ControllerContext>().Object;
            ViewEngineCollection collection = new ViewEngineCollection();
            Mock<IViewEngine> engine1 = new Mock<IViewEngine>();
            ViewEngineResult engine1Result = new ViewEngineResult(new[] { "location1", "location2" });
            engine1.Setup(e => e.FindView(context, "view", "master", It.IsAny<bool>())).Returns(engine1Result);
            Mock<IViewEngine> engine2 = new Mock<IViewEngine>();
            ViewEngineResult engine2Result = new ViewEngineResult(new Mock<IView>().Object, engine2.Object);
            engine2.Setup(e => e.FindView(context, "view", "master", It.IsAny<bool>())).Returns(engine2Result);
            collection.Add(engine1.Object);
            collection.Add(engine2.Object);

            // Act
            ViewEngineResult result = collection.FindView(context, "view", "master");

            // Assert
            Assert.Same(engine2Result, result);
        }
Esempio n. 40
0
        private static IView FindPartialView(ViewContext viewContext, string partialViewName, ViewEngineCollection viewEngineCollection, string masterName)
        {
            ViewEngineResult result = viewEngineCollection.FindView(viewContext, partialViewName, masterName);
            if (result.View != null)
            {
                return result.View;
            }

            StringBuilder locationsText = new StringBuilder();
            foreach (string location in result.SearchedLocations)
            {
                locationsText.AppendLine();
                locationsText.Append(location);
            }

            throw new InvalidOperationException(String.Format(CultureInfo.CurrentUICulture,
                "could not find view {0} looked in {1}", partialViewName, locationsText));
        }
        public void FindViewRemovesDuplicateSearchedLocationsFromMultipleEngines()
        {
            // Arrange
            ControllerContext context = new Mock<ControllerContext>().Object;
            Mock<IViewEngine> engine1 = new Mock<IViewEngine>();
            ViewEngineResult engine1Result = new ViewEngineResult(new[] { "repeatLocation", "location1" });
            engine1.Setup(e => e.FindView(context, "view", "master", It.IsAny<bool>())).Returns(engine1Result);
            Mock<IViewEngine> engine2 = new Mock<IViewEngine>();
            ViewEngineResult engine2Result = new ViewEngineResult(new[] { "location2", "repeatLocation" });
            engine2.Setup(e => e.FindView(context, "view", "master", It.IsAny<bool>())).Returns(engine2Result);
            ViewEngineCollection collection = new ViewEngineCollection()
            {
                engine1.Object,
                engine2.Object,
            };

            // Act
            ViewEngineResult result = collection.FindView(context, "view", "master");

            // Assert
            Assert.Null(result.View);
            var expectedLocations = new[] { "repeatLocation", "location1", "location2" };
            Assert.Equal(expectedLocations, result.SearchedLocations.ToArray());
        }
Esempio n. 42
0
        public void FindViewReturnsValueFromFirstSuccessfulEngine() {
            // Arrange
            ControllerContext context = new Mock<ControllerContext>().Object;
            ViewEngineCollection collection = new ViewEngineCollection();
            Mock<IViewEngine> engine1 = new Mock<IViewEngine>();
            ViewEngineResult engine1Result = new ViewEngineResult(new Mock<IView>().Object, engine1.Object);
            engine1.Expect(e => e.FindView(context, "view", "master", It.IsAny<bool>())).Returns(engine1Result);
            Mock<IViewEngine> engine2 = new Mock<IViewEngine>();
            ViewEngineResult engine2Result = new ViewEngineResult(new Mock<IView>().Object, engine2.Object);
            engine2.Expect(e => e.FindView(context, "view", "master", It.IsAny<bool>())).Returns(engine2Result);
            collection.Add(engine1.Object);
            collection.Add(engine2.Object);

            // Act
            ViewEngineResult result = collection.FindView(context, "view", "master");

            // Assert
            Assert.AreSame(engine1Result, result);
        }
        public void FindViewReturnsNoViewAndEmptySearchedLocationsIfCollectionEmpty()
        {
            // Arrange
            ControllerContext context = new Mock<ControllerContext>().Object;
            ViewEngineCollection collection = new ViewEngineCollection();

            // Act
            ViewEngineResult result = collection.FindView(context, "view", null);

            // Assert
            Assert.Null(result.View);
            Assert.Empty(result.SearchedLocations);
        }
        public void FindViewSuccessWithOneEngine()
        {
            // Arrange
            ControllerContext context = new Mock<ControllerContext>().Object;
            ViewEngineCollection collection = new ViewEngineCollection();
            Mock<IViewEngine> engine = new Mock<IViewEngine>();
            ViewEngineResult engineResult = new ViewEngineResult(new Mock<IView>().Object, engine.Object);
            engine.Setup(e => e.FindView(context, "view", "master", It.IsAny<bool>())).Returns(engineResult);
            collection.Add(engine.Object);

            // Act
            ViewEngineResult result = collection.FindView(context, "view", "master");

            // Assert
            Assert.Same(engineResult, result);
        }
        public void FindViewThrowsIfViewNameIsNull()
        {
            // Arrange
            ControllerContext context = new Mock<ControllerContext>().Object;
            ViewEngineCollection collection = new ViewEngineCollection();

            // Act & Assert
            Assert.ThrowsArgumentNullOrEmpty(
                () => collection.FindView(context, null, null),
                "viewName"
                );
        }
        public void FindViewThrowsIfControllerContextIsNull()
        {
            // Arrange
            ViewEngineCollection collection = new ViewEngineCollection();

            // Act & Assert
            Assert.ThrowsArgumentNull(
                () => collection.FindView(null, "view", null),
                "controllerContext"
                );
        }
        public void FindViewAggregatesAllSearchedLocationsIfAllEnginesFail()
        {
            // Arrange
            ControllerContext context = new Mock<ControllerContext>().Object;
            ViewEngineCollection collection = new ViewEngineCollection();
            Mock<IViewEngine> engine1 = new Mock<IViewEngine>();
            ViewEngineResult engine1Result = new ViewEngineResult(new[] { "location1", "location2" });
            engine1.Setup(e => e.FindView(context, "view", "master", It.IsAny<bool>())).Returns(engine1Result);
            Mock<IViewEngine> engine2 = new Mock<IViewEngine>();
            ViewEngineResult engine2Result = new ViewEngineResult(new[] { "location3", "location4" });
            engine2.Setup(e => e.FindView(context, "view", "master", It.IsAny<bool>())).Returns(engine2Result);
            collection.Add(engine1.Object);
            collection.Add(engine2.Object);

            // Act
            ViewEngineResult result = collection.FindView(context, "view", "master");

            // Assert
            Assert.Null(result.View);
            Assert.Equal(4, result.SearchedLocations.Count());
            Assert.True(result.SearchedLocations.Contains("location1"));
            Assert.True(result.SearchedLocations.Contains("location2"));
            Assert.True(result.SearchedLocations.Contains("location3"));
            Assert.True(result.SearchedLocations.Contains("location4"));
        }
Esempio n. 48
0
        public void FindViewThrowsIfViewNameIsEmpty() {
            // Arrange
            ControllerContext context = new Mock<ControllerContext>().Object;
            ViewEngineCollection collection = new ViewEngineCollection();

            // Act & Assert
            ExceptionHelper.ExpectArgumentExceptionNullOrEmpty(
                () => collection.FindView(context, "", null),
                "viewName"
            );
        }