public void GetView_ReturnsNotFound_IfAllViewEnginesReturnNotFound(bool isMainPage) { // Arrange var viewName = "foo.cshtml"; var expectedViewName = "~/" + viewName; 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.GetView("~/Index.html", viewName, isMainPage)) .Returns(ViewEngineResult.NotFound(expectedViewName, new[] { "1", "2" })); engine2 .Setup(e => e.GetView("~/Index.html", viewName, isMainPage)) .Returns(ViewEngineResult.NotFound(expectedViewName, new[] { "3" })); engine3 .Setup(e => e.GetView("~/Index.html", viewName, isMainPage)) .Returns(ViewEngineResult.NotFound(expectedViewName, new[] { "4", "5" })); var optionsAccessor = new TestOptionsManager <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.GetView("~/Index.html", viewName, isMainPage); // Assert Assert.False(result.Success); Assert.Equal(new[] { "1", "2", "3", "4", "5" }, result.SearchedLocations); }
/// <summary> Return a view by giving a specific file location. </summary> /// <param name="executingFilePath"> The absolute path to the currently-executing view, if any (sets a base path to locate the view by). </param> /// <param name="viewPath"> The relative path to the view (relative to 'executingFilePath', if given). </param> /// <param name="isMainPage"> Determines if the page being found is the main page for an action.. </param> /// <returns> The view. </returns> public ViewEngineResult GetView(string executingFilePath, string viewPath, bool isMainPage) { var result = _CompositeViewEngine.GetView(executingFilePath, viewPath, isMainPage); if (result.Success) { var newResult = ((result.View as RazorView)?.RazorPage as IViewPageRenderEvents)?.OnViewFound(_Services.GetService <IActionContextAccessor>()?.ActionContext, result); result = newResult ?? ViewEngineResult.Found(result.ViewName, new ViewResultProxy(result.View)); } return(result); }
public void GetView_ReturnsNotFoundResult_WhenNoViewEnginesAreRegistered(bool isMainPage) { // Arrange var viewName = "test-view.cshtml"; var optionsAccessor = new TestOptionsManager <MvcViewOptions>(); var compositeViewEngine = new CompositeViewEngine(optionsAccessor); // Act var result = compositeViewEngine.GetView("~/Index.html", viewName, isMainPage); // Assert Assert.False(result.Success); Assert.Empty(result.SearchedLocations); }
public void GetView_ReturnsNotFoundResult_WhenNoViewEnginesAreRegistered(bool isMainPage) { // 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.cshtml"; var optionsAccessor = Options.Create(new MvcViewOptions()); var compositeViewEngine = new CompositeViewEngine(optionsAccessor); // Act & Assert var exception = Assert.Throws <InvalidOperationException>( () => compositeViewEngine.GetView("~/Index.html", viewName, isMainPage)); Assert.Equal(expected, exception.Message); }
public void GetView_ReturnsView_WhenExactlyOneViewEngineIsRegisteredWhichReturnsAFoundResult(bool isMainPage) { // Arrange var viewName = "test-view.cshtml"; var expectedViewName = "~/" + viewName; var engine = new Mock <IViewEngine>(MockBehavior.Strict); var view = Mock.Of <IView>(); engine .Setup(e => e.GetView("~/Index.html", viewName, isMainPage)) .Returns(ViewEngineResult.Found(expectedViewName, view)); var optionsAccessor = Options.Create(new MvcViewOptions()); optionsAccessor.Value.ViewEngines.Add(engine.Object); var compositeViewEngine = new CompositeViewEngine(optionsAccessor); // Act var result = compositeViewEngine.GetView("~/Index.html", viewName, isMainPage); // Assert Assert.True(result.Success); Assert.Same(view, result.View); }
public void GetView_ReturnsNotFoundResult_WhenExactlyOneViewEngineIsRegisteredWhichReturnsNotFoundResult( bool isMainPage) { // Arrange var viewName = "test-view.cshtml"; var expectedViewName = "~/" + viewName; var engine = new Mock <IViewEngine>(MockBehavior.Strict); engine .Setup(e => e.GetView("~/Index.html", viewName, isMainPage)) .Returns(ViewEngineResult.NotFound(expectedViewName, new[] { expectedViewName })); var optionsAccessor = new TestOptionsManager <MvcViewOptions>(); optionsAccessor.Value.ViewEngines.Add(engine.Object); var compositeViewEngine = new CompositeViewEngine(optionsAccessor); // Act var result = compositeViewEngine.GetView("~/Index.html", viewName, isMainPage); // Assert Assert.False(result.Success); Assert.Equal(new[] { expectedViewName }, result.SearchedLocations); }
public void GetView_ReturnsViewFromFirstViewEngineWithFoundResult(bool isMainPage) { // Arrange var viewName = "foo.cshtml"; var expectedViewName = "~/" + viewName; 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.GetView("~/Index.html", viewName, isMainPage)) .Returns(ViewEngineResult.NotFound(expectedViewName, Enumerable.Empty <string>())); engine2 .Setup(e => e.GetView("~/Index.html", viewName, isMainPage)) .Returns(ViewEngineResult.Found(expectedViewName, view2)); engine3 .Setup(e => e.GetView("~/Index.html", viewName, isMainPage)) .Returns(ViewEngineResult.Found(expectedViewName, 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.GetView("~/Index.html", viewName, isMainPage); // Assert Assert.True(result.Success); Assert.Same(view2, result.View); Assert.Equal(expectedViewName, result.ViewName); }