public async Task ExecuteResultAsync_FindsAndExecutesView() { // Arrange var viewName = "MyView"; var actionContext = GetActionContext(); var view = new Mock <IView>(MockBehavior.Strict); view .Setup(v => v.RenderAsync(It.IsAny <ViewContext>())) .Returns(Task.FromResult(0)) .Verifiable(); view .As <IDisposable>() .Setup(v => v.Dispose()) .Verifiable(); // Used by logging view .SetupGet(v => v.Path) .Returns($"{viewName}.cshtml"); var viewEngine = new Mock <IViewEngine>(MockBehavior.Strict); viewEngine .Setup(v => v.GetView(/*executingFilePath*/ null, viewName, /*isMainPage*/ false)) .Returns(ViewEngineResult.NotFound(viewName, Enumerable.Empty <string>())) .Verifiable(); viewEngine .Setup(v => v.FindView(It.IsAny <ActionContext>(), viewName, /*isMainPage*/ false)) .Returns(ViewEngineResult.Found(viewName, view.Object)) .Verifiable(); var viewResult = new PartialViewResult { ViewName = viewName, ViewEngine = viewEngine.Object, ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()), TempData = Mock.Of <ITempDataDictionary>(), }; // Act await viewResult.ExecuteResultAsync(actionContext); // Assert view.Verify(); viewEngine.Verify(); }
public async Task ExecuteResultAsync_Throws_IfServicesNotRegistered() { // Arrange var actionContext = new ActionContext(new DefaultHttpContext() { RequestServices = Mock.Of <IServiceProvider>(), }, new RouteData(), new ActionDescriptor()); var expected = $"Unable to find the required services. Please add all the required services by calling " + $"'IServiceCollection.AddControllersWithViews()' inside the call to 'ConfigureServices(...)' " + $"in the application startup code."; var viewResult = new PartialViewResult(); // Act var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => viewResult.ExecuteResultAsync(actionContext)); // Assert Assert.Equal(expected, ex.Message); }
public async Task ExecuteResultAsync_Throws_IfViewCouldNotBeFound_MessageUsesAllLocations() { // Arrange var viewName = "MyView"; var actionContext = GetActionContext(); var expected = string.Join( Environment.NewLine, $"The view '{viewName}' was not found. The following locations were searched:", "Location1", "Location2", "Location3", "Location4"); var viewEngine = new Mock <IViewEngine>(MockBehavior.Strict); viewEngine .Setup(v => v.GetView(/*executingFilePath*/ null, viewName, /*isMainPage*/ false)) .Returns(ViewEngineResult.NotFound(viewName, new[] { "Location1", "Location2" })) .Verifiable(); viewEngine .Setup(v => v.FindView(It.IsAny <ActionContext>(), viewName, /*isMainPage*/ false)) .Returns(ViewEngineResult.NotFound(viewName, new[] { "Location3", "Location4" })) .Verifiable(); var viewResult = new PartialViewResult { ViewEngine = viewEngine.Object, ViewName = viewName, ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()), TempData = Mock.Of <ITempDataDictionary>(), }; // Act and Assert var ex = await Assert.ThrowsAsync <InvalidOperationException>( () => viewResult.ExecuteResultAsync(actionContext)); Assert.Equal(expected, ex.Message); viewEngine.Verify(); }