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);
        }
Example #2
0
        /// <summary>
        /// Executes the action result.
        /// </summary>
        /// <param name="controllerContext"></param>
        protected override void ExecuteInternal(ControllerContext controllerContext)
        {
            Guard.ArgumentNotNull(controllerContext, "controllerContext");

            var viewName = this.viewName ?? controllerContext.ActionName;
            ViewEngineResult = viewEngines.FindView(controllerContext, Options, viewName);
            if (ViewEngineResult.Success)
            {
                ViewEngineResult.Render();
            }
            else
            {
                throw new ViewNotFoundException(controllerContext.ControllerName, controllerContext.ActionName, viewName, ViewEngineResult.SearchLocations);
            }
        }
        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));
        }