public void FindView_ReturnsNotFound_IfAllViewEnginesReturnNotFound()
        {
            // Arrange
            var viewName = "foo";
            var engine1  = new Mock <IViewEngine>(MockBehavior.Strict);
            var engine2  = new Mock <IViewEngine>(MockBehavior.Strict);
            var engine3  = new Mock <IViewEngine>(MockBehavior.Strict);

            engine1
            .Setup(e => e.FindView(It.IsAny <ActionContext>(), viewName, /*isMainPage*/ false))
            .Returns(ViewEngineResult.NotFound(viewName, new[] { "1", "2" }));
            engine2
            .Setup(e => e.FindView(It.IsAny <ActionContext>(), viewName, /*isMainPage*/ false))
            .Returns(ViewEngineResult.NotFound(viewName, new[] { "3" }));
            engine3
            .Setup(e => e.FindView(It.IsAny <ActionContext>(), viewName, /*isMainPage*/ false))
            .Returns(ViewEngineResult.NotFound(viewName, new[] { "4", "5" }));

            var optionsAccessor = Options.Create(new MvcViewOptions());

            optionsAccessor.Value.ViewEngines.Add(engine1.Object);
            optionsAccessor.Value.ViewEngines.Add(engine2.Object);
            optionsAccessor.Value.ViewEngines.Add(engine3.Object);
            var compositeViewEngine = new CompositeViewEngine(optionsAccessor);

            // Act
            var result = compositeViewEngine.FindView(GetActionContext(), viewName, isMainPage: false);

            // Assert
            Assert.False(result.Success);
            Assert.Equal(new[] { "1", "2", "3", "4", "5" }, result.SearchedLocations);
        }
        public void FindView_ReturnsViewFromFirstViewEngineWithFoundResult()
        {
            // Arrange
            var viewName = "bar";
            var engine1  = new Mock <IViewEngine>(MockBehavior.Strict);
            var engine2  = new Mock <IViewEngine>(MockBehavior.Strict);
            var engine3  = new Mock <IViewEngine>(MockBehavior.Strict);
            var view2    = Mock.Of <IView>();
            var view3    = Mock.Of <IView>();

            engine1
            .Setup(e => e.FindView(It.IsAny <ActionContext>(), viewName, /*isMainPage*/ false))
            .Returns(ViewEngineResult.NotFound(viewName, Enumerable.Empty <string>()));
            engine2
            .Setup(e => e.FindView(It.IsAny <ActionContext>(), viewName, /*isMainPage*/ false))
            .Returns(ViewEngineResult.Found(viewName, view2));
            engine3
            .Setup(e => e.FindView(It.IsAny <ActionContext>(), viewName, /*isMainPage*/ false))
            .Returns(ViewEngineResult.Found(viewName, view3));

            var optionsAccessor = Options.Create(new MvcViewOptions());

            optionsAccessor.Value.ViewEngines.Add(engine1.Object);
            optionsAccessor.Value.ViewEngines.Add(engine2.Object);
            optionsAccessor.Value.ViewEngines.Add(engine3.Object);
            var compositeViewEngine = new CompositeViewEngine(optionsAccessor);

            // Act
            var result = compositeViewEngine.FindView(GetActionContext(), viewName, isMainPage: false);

            // Assert
            Assert.True(result.Success);
            Assert.Same(view2, result.View);
            Assert.Equal(viewName, result.ViewName);
        }
示例#3
0
        /// <summary> Locate a view by name using all view location search paths. </summary>
        /// <param name="context"> The controller action context. </param>
        /// <param name="viewName"> The name or path of the view that is rendered to the response. </param>
        /// <param name="isMainPage"> Determines if the page being found is the main page for an action.. </param>
        /// <returns> The found view. </returns>
        public ViewEngineResult FindView(ActionContext context, string viewName, bool isMainPage)
        {
            var result = _CompositeViewEngine.FindView(context, viewName, isMainPage);

            if (result.Success)
            {
                var newResult = ((result.View as RazorView)?.RazorPage as IViewPageRenderEvents)?.OnViewFound(context, result);
                result = newResult ?? ViewEngineResult.Found(result.ViewName, new ViewResultProxy(result.View));
            }
            return(result);
        }
        public void FindView_ReturnsNotFoundResult_WhenNoViewEnginesAreRegistered()
        {
            // Arrange
            var viewName            = "my-partial-view";
            var optionsAccessor     = new TestOptionsManager <MvcViewOptions>();
            var compositeViewEngine = new CompositeViewEngine(optionsAccessor);

            // Act
            var result = compositeViewEngine.FindView(GetActionContext(), viewName, isMainPage: false);

            // Assert
            Assert.False(result.Success);
            Assert.Empty(result.SearchedLocations);
        }
        public void FindView_ReturnsNotFoundResult_WhenNoViewEnginesAreRegistered()
        {
            // Arrange
            var expected = $"'{typeof(MvcViewOptions).FullName}.{nameof(MvcViewOptions.ViewEngines)}' must not be " +
                           $"empty. At least one '{typeof(IViewEngine).FullName}' is required to locate a view for rendering.";
            var viewName            = "my-partial-view";
            var optionsAccessor     = Options.Create(new MvcViewOptions());
            var compositeViewEngine = new CompositeViewEngine(optionsAccessor);

            // Act & AssertS
            var exception = Assert.Throws <InvalidOperationException>(
                () => compositeViewEngine.FindView(GetActionContext(), viewName, isMainPage: false));

            Assert.Equal(expected, exception.Message);
        }
示例#6
0
        public void FindView_IsMainPage_Throws_WhenNoViewEnginesAreRegistered()
        {
            // Arrange
            var expected = $"'{typeof(MvcViewOptions).FullName}.{nameof(MvcViewOptions.ViewEngines)}' must not be " +
                           $"empty. At least one '{typeof(IViewEngine).FullName}' is required to locate a view for rendering.";
            var viewName            = "test-view";
            var actionContext       = GetActionContext();
            var optionsAccessor     = new TestOptionsManager <MvcViewOptions>();
            var compositeViewEngine = new CompositeViewEngine(optionsAccessor);

            // Act & Assert
            var exception = Assert.Throws <InvalidOperationException>(
                () => compositeViewEngine.FindView(actionContext, viewName, isMainPage: true));

            Assert.Equal(expected, exception.Message);
        }
        public void FindView_IsMainPage_ReturnsNotFoundResult_WhenExactlyOneViewEngineIsRegisteredWhichReturnsNotFoundResult()
        {
            // Arrange
            var viewName = "test-view";
            var engine   = new Mock <IViewEngine>(MockBehavior.Strict);

            engine
            .Setup(e => e.FindView(It.IsAny <ActionContext>(), viewName, /*isMainPage*/ true))
            .Returns(ViewEngineResult.NotFound(viewName, new[] { "controller/test-view" }));
            var optionsAccessor = Options.Create(new MvcViewOptions());

            optionsAccessor.Value.ViewEngines.Add(engine.Object);
            var compositeViewEngine = new CompositeViewEngine(optionsAccessor);

            // Act
            var result = compositeViewEngine.FindView(GetActionContext(), viewName, isMainPage: true);

            // Assert
            Assert.False(result.Success);
            Assert.Equal(new[] { "controller/test-view" }, result.SearchedLocations);
        }
        public void FindView_IsMainPage_ReturnsView_WhenExactlyOneViewEngineIsRegisteredWhichReturnsAFoundResult()
        {
            // Arrange
            var viewName = "test-view";
            var engine   = new Mock <IViewEngine>(MockBehavior.Strict);
            var view     = Mock.Of <IView>();

            engine
            .Setup(e => e.FindView(It.IsAny <ActionContext>(), viewName, /*isMainPage*/ true))
            .Returns(ViewEngineResult.Found(viewName, view));
            var optionsAccessor = Options.Create(new MvcViewOptions());

            optionsAccessor.Value.ViewEngines.Add(engine.Object);
            var compositeViewEngine = new CompositeViewEngine(optionsAccessor);

            // Act
            var result = compositeViewEngine.FindView(GetActionContext(), viewName, isMainPage: true);

            // Assert
            Assert.True(result.Success);
            Assert.Same(view, result.View);
        }