diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PartialViewResultTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/PartialViewResultTest.cs similarity index 82% rename from test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PartialViewResultTest.cs rename to test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/PartialViewResultTest.cs index 7b82f63066..5acbdb5fa6 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/PartialViewResultTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/PartialViewResultTest.cs @@ -10,13 +10,15 @@ using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.ViewEngines; +using Microsoft.AspNetCore.Mvc.ViewFeatures; +using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging.Testing; using Moq; using Xunit; -namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal +namespace Microsoft.AspNetCore.Mvc { // These tests cover the logic included in PartialViewResult.ExecuteResultAsync - see PartialViewResultExecutorTest // and ViewExecutorTest for more comprehensive tests. @@ -43,28 +45,29 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal public async Task ExecuteResultAsync_Throws_IfViewCouldNotBeFound_MessageUsesGetViewLocations() { // Arrange + var viewName = "MyView"; + var actionContext = GetActionContext(); var expected = string.Join( Environment.NewLine, - "The view 'MyView' was not found. The following locations were searched:", + $"The view '{viewName}' was not found. The following locations were searched:", "Location1", "Location2"); - var actionContext = GetActionContext(); - var viewEngine = new Mock(MockBehavior.Strict); viewEngine - .Setup(v => v.GetView(/*executingFilePath*/ null, "MyView", /*isMainPage*/ false)) - .Returns(ViewEngineResult.NotFound("MyView", new[] { "Location1", "Location2" })) + .Setup(v => v.GetView(/*executingFilePath*/ null, viewName, /*isMainPage*/ false)) + .Returns(ViewEngineResult.NotFound(viewName, new[] { "Location1", "Location2" })) .Verifiable(); + viewEngine - .Setup(v => v.FindView(It.IsAny(), "MyView", /*isMainPage*/ false)) - .Returns(ViewEngineResult.NotFound("MyView", Enumerable.Empty())) + .Setup(v => v.FindView(It.IsAny(), viewName, /*isMainPage*/ false)) + .Returns(ViewEngineResult.NotFound(viewName, Enumerable.Empty())) .Verifiable(); var viewResult = new PartialViewResult { ViewEngine = viewEngine.Object, - ViewName = "MyView", + ViewName = viewName, ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()), TempData = Mock.Of(), }; @@ -80,28 +83,29 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal public async Task ExecuteResultAsync_Throws_IfViewCouldNotBeFound_MessageUsesFindViewLocations() { // Arrange + var viewName = "MyView"; + var actionContext = GetActionContext(); var expected = string.Join( Environment.NewLine, - "The view 'MyView' was not found. The following locations were searched:", + $"The view '{viewName}' was not found. The following locations were searched:", "Location1", "Location2"); - var actionContext = GetActionContext(); - var viewEngine = new Mock(MockBehavior.Strict); viewEngine - .Setup(v => v.GetView(/*executingFilePath*/ null, "MyView", /*isMainPage*/ false)) - .Returns(ViewEngineResult.NotFound("MyView", Enumerable.Empty())) + .Setup(v => v.GetView(/*executingFilePath*/ null, viewName, /*isMainPage*/ false)) + .Returns(ViewEngineResult.NotFound(viewName, Enumerable.Empty())) .Verifiable(); + viewEngine - .Setup(v => v.FindView(It.IsAny(), "MyView", /*isMainPage*/ false)) - .Returns(ViewEngineResult.NotFound("MyView", new[] { "Location1", "Location2" })) + .Setup(v => v.FindView(It.IsAny(), viewName, /*isMainPage*/ false)) + .Returns(ViewEngineResult.NotFound(viewName, new[] { "Location1", "Location2" })) .Verifiable(); var viewResult = new PartialViewResult { ViewEngine = viewEngine.Object, - ViewName = "MyView", + ViewName = viewName, ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()), TempData = Mock.Of(), }; @@ -117,30 +121,31 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal public async Task ExecuteResultAsync_Throws_IfViewCouldNotBeFound_MessageUsesAllLocations() { // Arrange + var viewName = "MyView"; + var actionContext = GetActionContext(); var expected = string.Join( Environment.NewLine, - "The view 'MyView' was not found. The following locations were searched:", + $"The view '{viewName}' was not found. The following locations were searched:", "Location1", "Location2", "Location3", "Location4"); - var actionContext = GetActionContext(); - var viewEngine = new Mock(MockBehavior.Strict); viewEngine - .Setup(v => v.GetView(/*executingFilePath*/ null, "MyView", /*isMainPage*/ false)) - .Returns(ViewEngineResult.NotFound("MyView", new[] { "Location1", "Location2" })) + .Setup(v => v.GetView(/*executingFilePath*/ null, viewName, /*isMainPage*/ false)) + .Returns(ViewEngineResult.NotFound(viewName, new[] { "Location1", "Location2" })) .Verifiable(); + viewEngine - .Setup(v => v.FindView(It.IsAny(), "MyView", /*isMainPage*/ false)) - .Returns(ViewEngineResult.NotFound("MyView", new[] { "Location3", "Location4" })) + .Setup(v => v.FindView(It.IsAny(), viewName, /*isMainPage*/ false)) + .Returns(ViewEngineResult.NotFound(viewName, new[] { "Location3", "Location4" })) .Verifiable(); var viewResult = new PartialViewResult { ViewEngine = viewEngine.Object, - ViewName = "MyView", + ViewName = viewName, ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()), TempData = Mock.Of(), }; @@ -156,8 +161,8 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal public async Task ExecuteResultAsync_FindsAndExecutesView() { // Arrange - var viewName = "myview"; - var context = GetActionContext(); + var viewName = "MyView"; + var actionContext = GetActionContext(); var view = new Mock(MockBehavior.Strict); view @@ -173,16 +178,17 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal // Used by logging view .SetupGet(v => v.Path) - .Returns("myview.cshtml"); + .Returns($"{viewName}.cshtml"); var viewEngine = new Mock(MockBehavior.Strict); viewEngine - .Setup(v => v.GetView(/*executingFilePath*/ null, "myview", /*isMainPage*/ false)) - .Returns(ViewEngineResult.NotFound("myview", Enumerable.Empty())) + .Setup(v => v.GetView(/*executingFilePath*/ null, viewName, /*isMainPage*/ false)) + .Returns(ViewEngineResult.NotFound(viewName, Enumerable.Empty())) .Verifiable(); + viewEngine - .Setup(v => v.FindView(It.IsAny(), "myview", /*isMainPage*/ false)) - .Returns(ViewEngineResult.Found("myview", view.Object)) + .Setup(v => v.FindView(It.IsAny(), viewName, /*isMainPage*/ false)) + .Returns(ViewEngineResult.Found(viewName, view.Object)) .Verifiable(); var viewResult = new PartialViewResult @@ -194,7 +200,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal }; // Act - await viewResult.ExecuteResultAsync(context); + await viewResult.ExecuteResultAsync(actionContext); // Assert view.Verify(); diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewResultTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewResultTest.cs index d9bf6609ae..d7b2723a2d 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewResultTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewResultTest.cs @@ -55,28 +55,29 @@ namespace Microsoft.AspNetCore.Mvc public async Task ExecuteResultAsync_Throws_IfViewCouldNotBeFound_MessageUsesGetViewLocations() { // Arrange + var viewName = "MyView"; + var actionContext = GetActionContext(); var expected = string.Join( Environment.NewLine, - "The view 'MyView' was not found. The following locations were searched:", + $"The view '{viewName}' was not found. The following locations were searched:", "Location1", "Location2"); - var actionContext = GetActionContext(); - var viewEngine = new Mock(MockBehavior.Strict); viewEngine - .Setup(e => e.GetView(/*executingFilePath*/ null, "MyView", /*isMainPage*/ true)) - .Returns(ViewEngineResult.NotFound("MyView", new[] { "Location1", "Location2" })) + .Setup(v => v.GetView(/*executingFilePath*/ null, viewName, /*isMainPage*/ true)) + .Returns(ViewEngineResult.NotFound(viewName, Enumerable.Empty())) .Verifiable(); + viewEngine - .Setup(v => v.FindView(It.IsAny(), It.IsAny(), /*isMainPage*/ true)) - .Returns(ViewEngineResult.NotFound("MyView", Enumerable.Empty())) + .Setup(v => v.FindView(It.IsAny(), viewName, /*isMainPage*/ true)) + .Returns(ViewEngineResult.NotFound(viewName, new[] { "Location1", "Location2" })) .Verifiable(); var viewResult = new ViewResult { ViewEngine = viewEngine.Object, - ViewName = "MyView", + ViewName = viewName, ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()), TempData = Mock.Of(), }; @@ -92,28 +93,31 @@ namespace Microsoft.AspNetCore.Mvc public async Task ExecuteResultAsync_Throws_IfViewCouldNotBeFound_MessageUsesFindViewLocations() { // Arrange + var viewName = "MyView"; + var actionContext = GetActionContext(); var expected = string.Join( Environment.NewLine, - "The view 'MyView' was not found. The following locations were searched:", + $"The view '{viewName}' was not found. The following locations were searched:", "Location1", - "Location2"); - - var actionContext = GetActionContext(); + "Location2", + "Location3", + "Location4"); var viewEngine = new Mock(MockBehavior.Strict); viewEngine - .Setup(e => e.GetView(/*executingFilePath*/ null, "MyView", /*isMainPage*/ true)) - .Returns(ViewEngineResult.NotFound("MyView", Enumerable.Empty())) + .Setup(v => v.GetView(/*executingFilePath*/ null, viewName, /*isMainPage*/ true)) + .Returns(ViewEngineResult.NotFound(viewName, new[] { "Location1", "Location2" })) .Verifiable(); + viewEngine - .Setup(v => v.FindView(It.IsAny(), It.IsAny(), /*isMainPage*/ true)) - .Returns(ViewEngineResult.NotFound("MyView", new[] { "Location1", "Location2" })) + .Setup(v => v.FindView(It.IsAny(), viewName, /*isMainPage*/ true)) + .Returns(ViewEngineResult.NotFound(viewName, new[] { "Location3", "Location4" })) .Verifiable(); var viewResult = new ViewResult { ViewEngine = viewEngine.Object, - ViewName = "MyView", + ViewName = viewName, ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()), TempData = Mock.Of(), }; @@ -129,30 +133,31 @@ namespace Microsoft.AspNetCore.Mvc public async Task ExecuteResultAsync_Throws_IfViewCouldNotBeFound_MessageUsesAllLocations() { // Arrange + var viewName = "MyView"; + var actionContext = GetActionContext(); var expected = string.Join( Environment.NewLine, - "The view 'MyView' was not found. The following locations were searched:", + $"The view '{viewName}' was not found. The following locations were searched:", "Location1", "Location2", "Location3", "Location4"); - var actionContext = GetActionContext(); - var viewEngine = new Mock(MockBehavior.Strict); viewEngine - .Setup(e => e.GetView(/*executingFilePath*/ null, "MyView", /*isMainPage*/ true)) - .Returns(ViewEngineResult.NotFound("MyView", new[] { "Location1", "Location2" })) + .Setup(v => v.GetView(/*executingFilePath*/ null, viewName, /*isMainPage*/ true)) + .Returns(ViewEngineResult.NotFound(viewName, new[] { "Location1", "Location2" })) .Verifiable(); + viewEngine - .Setup(v => v.FindView(It.IsAny(), It.IsAny(), /*isMainPage*/ true)) - .Returns(ViewEngineResult.NotFound("MyView", new[] { "Location3", "Location4" })) + .Setup(v => v.FindView(It.IsAny(), viewName, /*isMainPage*/ true)) + .Returns(ViewEngineResult.NotFound(viewName, new[] { "Location3", "Location4" })) .Verifiable(); var viewResult = new ViewResult { ViewEngine = viewEngine.Object, - ViewName = "MyView", + ViewName = viewName, ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()), TempData = Mock.Of(), }; @@ -168,8 +173,8 @@ namespace Microsoft.AspNetCore.Mvc public async Task ExecuteResultAsync_FindsAndExecutesView() { // Arrange - var viewName = "myview"; - var context = GetActionContext(); + var viewName = "MyView"; + var actionContext = GetActionContext(); var view = new Mock(MockBehavior.Strict); view @@ -182,18 +187,20 @@ namespace Microsoft.AspNetCore.Mvc .Setup(v => v.Dispose()) .Verifiable(); + // Used by logging view - .Setup(v => v.Path) - .Returns("//location"); + .SetupGet(v => v.Path) + .Returns($"{viewName}.cshtml"); var viewEngine = new Mock(MockBehavior.Strict); viewEngine - .Setup(e => e.GetView(/*executingFilePath*/ null, "myview", /*isMainPage*/ true)) - .Returns(ViewEngineResult.NotFound("myview", Enumerable.Empty())) + .Setup(v => v.GetView(/*executingFilePath*/ null, viewName, /*isMainPage*/ true)) + .Returns(ViewEngineResult.NotFound(viewName, Enumerable.Empty())) .Verifiable(); + viewEngine - .Setup(e => e.FindView(context, "myview", /*isMainPage*/ true)) - .Returns(ViewEngineResult.Found("myview", view.Object)) + .Setup(v => v.FindView(It.IsAny(), viewName, /*isMainPage*/ true)) + .Returns(ViewEngineResult.Found(viewName, view.Object)) .Verifiable(); var viewResult = new ViewResult @@ -205,7 +212,7 @@ namespace Microsoft.AspNetCore.Mvc }; // Act - await viewResult.ExecuteResultAsync(context); + await viewResult.ExecuteResultAsync(actionContext); // Assert view.Verify();