Ejemplo n.º 1
0
        public async Task ExecuteResultAsync_Throws_IfViewCouldNotBeFound_MessageUsesGetViewLocations()
        {
            // Arrange
            var expected = string.Join(
                Environment.NewLine,
                "The view 'MyView' was not found. The following locations were searched:",
                "Location1",
                "Location2");

            var actionContext = GetActionContext();

            var viewEngine = new Mock<IViewEngine>(MockBehavior.Strict);
            viewEngine
                .Setup(e => e.GetView(/*executingFilePath*/ null, "MyView", /*isMainPage*/ true))
                .Returns(ViewEngineResult.NotFound("MyView", new[] { "Location1", "Location2" }))
                .Verifiable();
            viewEngine
                .Setup(v => v.FindView(It.IsAny<ActionContext>(), It.IsAny<string>(), /*isMainPage*/ true))
                .Returns(ViewEngineResult.NotFound("MyView", Enumerable.Empty<string>()))
                .Verifiable();

            var viewResult = new ViewResult
            {
                ViewEngine = viewEngine.Object,
                ViewName = "MyView",
                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();
        }
Ejemplo n.º 2
0
        public async Task ExecuteResultAsync_ReturnsError_IfViewCouldNotBeFound()
        {
            // Arrange
            var expected = string.Join(Environment.NewLine,
                                       "The view 'MyView' was not found. The following locations were searched:",
                                       "Location1",
                                       "Location2.");

            var actionContext = new ActionContext(GetHttpContext(),
                                                  new RouteData(),
                                                  new ActionDescriptor());
            var viewEngine = new Mock <IViewEngine>();

            viewEngine.Setup(v => v.FindView(It.IsAny <ActionContext>(), It.IsAny <string>()))
            .Returns(ViewEngineResult.NotFound("MyView", new[] { "Location1", "Location2" }))
            .Verifiable();

            var viewResult = new ViewResult
            {
                ViewEngine = viewEngine.Object,
                ViewName   = "MyView"
            };

            // Act and Assert
            var ex = await Assert.ThrowsAsync <InvalidOperationException>(
                () => viewResult.ExecuteResultAsync(actionContext));

            Assert.Equal(expected, ex.Message);
            viewEngine.Verify();
        }
Ejemplo n.º 3
0
        public async Task ExecuteResultAsync_Throws_IfViewCouldNotBeFound()
        {
            // Arrange
            var expected = string.Join(
                Environment.NewLine,
                "The view 'MyView' was not found. The following locations were searched:",
                "Location1",
                "Location2.");

            var actionContext = GetActionContext();

            var viewEngine = new Mock <IViewEngine>();

            viewEngine
            .Setup(v => v.FindView(It.IsAny <ActionContext>(), It.IsAny <string>()))
            .Returns(ViewEngineResult.NotFound("MyView", new[] { "Location1", "Location2" }))
            .Verifiable();

            var viewResult = new ViewResult
            {
                ViewEngine = viewEngine.Object,
                ViewName   = "MyView",
                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();
        }
Ejemplo n.º 4
0
        public async Task ViewResult_UsesFindViewOnSpecifiedViewEngineToLocateViews()
        {
            // Arrange
            var viewName = "myview";
            var context = new ActionContext(GetHttpContext(), new RouteData(), new ActionDescriptor());
            var viewEngine = new Mock<IViewEngine>();
            var view = Mock.Of<IView>();

            viewEngine
                .Setup(e => e.FindView(context, "myview"))
                .Returns(ViewEngineResult.Found("myview", view))
                .Verifiable();

            var viewResult = new ViewResult
            {
                ViewName = viewName,
                ViewEngine = viewEngine.Object,
                ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()),
                TempData = Mock.Of<ITempDataDictionary>(),
            };

            // Act
            await viewResult.ExecuteResultAsync(context);

            // Assert
            viewEngine.Verify();
        }
Ejemplo n.º 5
0
        public async Task ViewResult_NotifiesViewNotFound()
        {
            // Arrange
            var viewName    = "myview";
            var httpContext = GetHttpContext();
            var context     = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());

            var listener = new TestNotificationListener();

            httpContext.RequestServices.GetRequiredService <INotifier>().EnlistTarget(listener);

            var viewEngine = new Mock <IViewEngine>();
            var view       = Mock.Of <IView>();

            viewEngine.Setup(e => e.FindView(context, "myview"))
            .Returns(ViewEngineResult.NotFound("myview", new string[] { "location/myview" }));

            var viewResult = new ViewResult
            {
                ViewName   = viewName,
                ViewEngine = viewEngine.Object,
            };

            // Act
            await Assert.ThrowsAsync <InvalidOperationException>(
                async() => await viewResult.ExecuteResultAsync(context));

            // Assert
            Assert.NotNull(listener.ViewResultViewNotFound);
            Assert.NotNull(listener.ViewResultViewNotFound.ActionContext);
            Assert.NotNull(listener.ViewResultViewNotFound.Result);
            Assert.Equal(new string[] { "location/myview" }, listener.ViewResultViewNotFound.SearchedLocations);
            Assert.Equal("myview", listener.ViewResultViewNotFound.ViewName);
        }
Ejemplo n.º 6
0
        public async Task ViewResult_SetsContentTypeHeader(
            MediaTypeHeaderValue contentType,
            string expectedContentTypeHeaderValue)
        {
            // Arrange
            var viewName    = "myview";
            var httpContext = GetHttpContext();
            var context     = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
            var viewEngine  = new Mock <IViewEngine>();
            var view        = Mock.Of <IView>();
            var contentTypeBeforeViewResultExecution = contentType?.ToString();

            viewEngine.Setup(e => e.FindView(context, "myview"))
            .Returns(ViewEngineResult.Found("myview", view));

            var viewResult = new ViewResult
            {
                ViewName    = viewName,
                ViewEngine  = viewEngine.Object,
                ContentType = contentType
            };

            // Act
            await viewResult.ExecuteResultAsync(context);

            // Assert
            Assert.Equal(expectedContentTypeHeaderValue, httpContext.Response.ContentType);

            // Check if the original instance provided by the user has not changed.
            // Since we do not have access to the new instance created within the view executor,
            // check if at least the content is the same.
            var contentTypeAfterViewResultExecution = contentType?.ToString();

            Assert.Equal(contentTypeBeforeViewResultExecution, contentTypeAfterViewResultExecution);
        }
Ejemplo n.º 7
0
        public async Task ViewResult_SetsStatusCode()
        {
            // Arrange
            var viewName    = "myview";
            var httpContext = GetHttpContext();
            var context     = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
            var viewEngine  = new Mock <IViewEngine>();
            var view        = Mock.Of <IView>();

            viewEngine.Setup(e => e.FindView(context, "myview"))
            .Returns(ViewEngineResult.Found("myview", view));

            var viewResult = new ViewResult
            {
                ViewName   = viewName,
                ViewEngine = viewEngine.Object,
                StatusCode = 404,
            };

            // Act
            await viewResult.ExecuteResultAsync(context);

            // Assert
            Assert.Equal(404, httpContext.Response.StatusCode);
        }
Ejemplo n.º 8
0
        public async Task ExecuteResultAsync_UsesCompositeViewEngineFromServices_IfViewEngineIsNotSpecified()
        {
            // Arrange
            var viewName = "some-view-name";
            var context  = new ActionContext(new DefaultHttpContext(),
                                             new RouteData(),
                                             new ActionDescriptor {
                Name = viewName
            });
            var viewEngine = new Mock <ICompositeViewEngine>();

            viewEngine.Setup(e => e.FindView(context, viewName))
            .Returns(ViewEngineResult.Found(viewName, Mock.Of <IView>()))
            .Verifiable();

            var serviceProvider = new Mock <IServiceProvider>();

            serviceProvider.Setup(p => p.GetService(typeof(ICompositeViewEngine)))
            .Returns(viewEngine.Object);
            context.HttpContext.RequestServices = serviceProvider.Object;

            var viewResult = new ViewResult
            {
                ViewName = viewName
            };

            // Act
            await viewResult.ExecuteResultAsync(context);

            // Assert
            viewEngine.Verify();
        }
Ejemplo n.º 9
0
        public async Task ExecuteResultAsync_ReturnsError_IfViewCouldNotBeFound()
        {
            // Arrange
            var expected = string.Join(Environment.NewLine,
                                       "The view 'MyView' was not found. The following locations were searched:",
                                       "Location1",
                                       "Location2.");
            
            var actionContext = new ActionContext(GetHttpContext(),
                                                  new RouteData(),
                                                  new ActionDescriptor());
            var viewEngine = new Mock<IViewEngine>();
            viewEngine.Setup(v => v.FindView(It.IsAny<ActionContext>(), It.IsAny<string>()))
                      .Returns(ViewEngineResult.NotFound("MyView", new[] { "Location1", "Location2" }))
                       .Verifiable();

            var viewResult = new ViewResult
            {
                ViewEngine = viewEngine.Object,
                ViewName = "MyView"
            };

            // Act and Assert
            var ex = await Assert.ThrowsAsync<InvalidOperationException>(
                                () => viewResult.ExecuteResultAsync(actionContext));
            Assert.Equal(expected, ex.Message);
            viewEngine.Verify();
        }
Ejemplo n.º 10
0
        public async Task ExecuteResultAsync_UsesActionDescriptorName_IfViewNameIsNull()
        {
            // Arrange
            var viewName = "some-view-name";
            var context  = new ActionContext(GetHttpContext(),
                                             new RouteData(),
                                             new ActionDescriptor {
                Name = viewName
            });
            var viewEngine = new Mock <ICompositeViewEngine>();

            viewEngine.Setup(e => e.FindView(context, viewName))
            .Returns(ViewEngineResult.Found(viewName, Mock.Of <IView>()))
            .Verifiable();

            var viewResult = new ViewResult
            {
                ViewEngine = viewEngine.Object
            };

            // Act
            await viewResult.ExecuteResultAsync(context);

            // Assert
            viewEngine.Verify();
        }
Ejemplo n.º 11
0
        public async Task ViewResult_UsesFindViewOnSpecifiedViewEngineToLocateViews()
        {
            // Arrange
            var viewName   = "myview";
            var context    = new ActionContext(GetHttpContext(), new RouteData(), new ActionDescriptor());
            var viewEngine = new Mock <IViewEngine>();
            var view       = Mock.Of <IView>();

            viewEngine
            .Setup(e => e.FindView(context, "myview"))
            .Returns(ViewEngineResult.Found("myview", view))
            .Verifiable();

            var viewResult = new ViewResult
            {
                ViewName   = viewName,
                ViewEngine = viewEngine.Object,
                ViewData   = new ViewDataDictionary(new EmptyModelMetadataProvider()),
                TempData   = Mock.Of <ITempDataDictionary>(),
            };

            // Act
            await viewResult.ExecuteResultAsync(context);

            // Assert
            viewEngine.Verify();
        }
Ejemplo n.º 12
0
        public async Task ViewResult_NotifiesViewFound()
        {
            // Arrange
            var viewName    = "myview";
            var httpContext = GetHttpContext();
            var context     = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());

            var listener = new TestTelemetryListener();

            httpContext.RequestServices.GetRequiredService <TelemetryListener>().SubscribeWithAdapter(listener);

            var viewEngine = new Mock <IViewEngine>();
            var view       = Mock.Of <IView>();

            viewEngine.Setup(e => e.FindView(context, "myview"))
            .Returns(ViewEngineResult.Found("myview", view));

            var viewResult = new ViewResult
            {
                ViewName   = viewName,
                ViewEngine = viewEngine.Object,
                ViewData   = new ViewDataDictionary(new EmptyModelMetadataProvider()),
                TempData   = Mock.Of <ITempDataDictionary>(),
            };

            // Act
            await viewResult.ExecuteResultAsync(context);

            // Assert
            Assert.NotNull(listener.ViewResultViewFound);
            Assert.NotNull(listener.ViewResultViewFound.ActionContext);
            Assert.NotNull(listener.ViewResultViewFound.Result);
            Assert.NotNull(listener.ViewResultViewFound.View);
            Assert.Equal("myview", listener.ViewResultViewFound.ViewName);
        }
Ejemplo n.º 13
0
        public async Task ExecuteResultAsync_UsesCompositeViewEngineFromServices_IfViewEngineIsNotSpecified()
        {
            // Arrange
            var viewName = "some-view-name";
            var context  = new ActionContext(new DefaultHttpContext(),
                                             new RouteData(),
                                             new ActionDescriptor {
                Name = viewName
            });
            var viewEngine = new Mock <ICompositeViewEngine>();

            viewEngine.Setup(e => e.FindView(context, viewName))
            .Returns(ViewEngineResult.Found(viewName, Mock.Of <IView>()))
            .Verifiable();

            var serviceProvider = new Mock <IServiceProvider>();

            var telemetry = new TelemetryListener("Microsoft.AspNet");

            serviceProvider
            .Setup(s => s.GetService(typeof(TelemetrySource)))
            .Returns(telemetry);
            serviceProvider
            .Setup(s => s.GetService(typeof(TelemetryListener)))
            .Returns(telemetry);
            serviceProvider.Setup(p => p.GetService(typeof(ICompositeViewEngine)))
            .Returns(viewEngine.Object);
            serviceProvider.Setup(p => p.GetService(typeof(ILogger <ViewResult>)))
            .Returns(new Mock <ILogger <ViewResult> >().Object);
            serviceProvider.Setup(s => s.GetService(typeof(IOptions <MvcViewOptions>)))
            .Returns(() => {
                var optionsAccessor = new Mock <IOptions <MvcViewOptions> >();
                optionsAccessor.SetupGet(o => o.Value)
                .Returns(new MvcViewOptions());
                return(optionsAccessor.Object);
            });
            context.HttpContext.RequestServices = serviceProvider.Object;

            var viewResult = new ViewResult
            {
                ViewName = viewName,
                ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()),
                TempData = Mock.Of <ITempDataDictionary>(),
            };

            // Act
            await viewResult.ExecuteResultAsync(context);

            // Assert
            viewEngine.Verify();
        }
Ejemplo n.º 14
0
        public async Task ExecuteResultAsync_FindsAndExecutesView()
        {
            // Arrange
            var viewName = "myview";
            var context  = 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();

            view
            .Setup(v => v.Path)
            .Returns("//location");

            var viewEngine = new Mock <IViewEngine>(MockBehavior.Strict);

            viewEngine
            .Setup(e => e.GetView(/*executingFilePath*/ null, "myview", /*isMainPage*/ true))
            .Returns(ViewEngineResult.NotFound("myview", Enumerable.Empty <string>()))
            .Verifiable();
            viewEngine
            .Setup(e => e.FindView(context, "myview", /*isMainPage*/ true))
            .Returns(ViewEngineResult.Found("myview", view.Object))
            .Verifiable();

            var viewResult = new ViewResult
            {
                ViewName   = viewName,
                ViewEngine = viewEngine.Object,
                ViewData   = new ViewDataDictionary(new EmptyModelMetadataProvider()),
                TempData   = Mock.Of <ITempDataDictionary>(),
            };

            // Act
            await viewResult.ExecuteResultAsync(context);

            // Assert
            view.Verify();
            viewEngine.Verify();
        }
Ejemplo n.º 15
0
        public async Task ExecuteResultAsync_UsesCompositeViewEngineFromServices_IfViewEngineIsNotSpecified()
        {
            // Arrange
            var viewName = "some-view-name";
            var context  = new ActionContext(new DefaultHttpContext(),
                                             new RouteData(),
                                             new ActionDescriptor {
                Name = viewName
            });
            var viewEngine = new Mock <ICompositeViewEngine>();

            viewEngine.Setup(e => e.FindView(context, viewName))
            .Returns(ViewEngineResult.Found(viewName, Mock.Of <IView>()))
            .Verifiable();

            var serviceProvider = new Mock <IServiceProvider>();

            serviceProvider.Setup(s => s.GetService(typeof(INotifier)))
            .Returns(new Notifier(new ProxyNotifierMethodAdapter()));
            serviceProvider.Setup(p => p.GetService(typeof(ICompositeViewEngine)))
            .Returns(viewEngine.Object);
            serviceProvider.Setup(p => p.GetService(typeof(ILogger <ViewResult>)))
            .Returns(new Mock <ILogger <ViewResult> >().Object);
            serviceProvider.Setup(s => s.GetService(typeof(IOptions <MvcViewOptions>)))
            .Returns(() => {
                var optionsAccessor = new Mock <IOptions <MvcViewOptions> >();
                optionsAccessor.SetupGet(o => o.Options)
                .Returns(new MvcViewOptions());
                return(optionsAccessor.Object);
            });
            context.HttpContext.RequestServices = serviceProvider.Object;

            var viewResult = new ViewResult
            {
                ViewName = viewName
            };

            // Act
            await viewResult.ExecuteResultAsync(context);

            // Assert
            viewEngine.Verify();
        }
        public override Task WriteResponseBodyAsync(OutputFormatterContext context)
        {
            var services = context.HttpContext.RequestServices;
            var httpContextAccessor = services.GetRequiredService<IHttpContextAccessor>();
            var actionContext = services.GetRequiredService<IActionContextAccessor>().ActionContext;
            var metadataProvider = services.GetRequiredService<IModelMetadataProvider>();
            var tempdataProvider = services.GetRequiredService<ITempDataProvider>();

            var viewResult = new ViewResult
            {
                ViewData = new ViewDataDictionary(metadataProvider, actionContext.ModelState)
                {
                    Model = context.Object
                },
                TempData = new TempDataDictionary(httpContextAccessor, tempdataProvider)
            };

            return viewResult.ExecuteResultAsync(actionContext);
        }
Ejemplo n.º 17
0
        public async Task ExecuteResultAsync_FindsAndExecutesView()
        {
            // Arrange
            var viewName = "myview";
            var context = 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();

            view
                .Setup(v => v.Path)
                .Returns("//location");

            var viewEngine = new Mock<IViewEngine>(MockBehavior.Strict);
            viewEngine
                .Setup(e => e.GetView(/*executingFilePath*/ null, "myview", /*isMainPage*/ true))
                .Returns(ViewEngineResult.NotFound("myview", Enumerable.Empty<string>()))
                .Verifiable();
            viewEngine
                .Setup(e => e.FindView(context, "myview", /*isMainPage*/ true))
                .Returns(ViewEngineResult.Found("myview", view.Object))
                .Verifiable();

            var viewResult = new ViewResult
            {
                ViewName = viewName,
                ViewEngine = viewEngine.Object,
                ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()),
                TempData = Mock.Of<ITempDataDictionary>(),
            };

            // Act
            await viewResult.ExecuteResultAsync(context);

            // Assert
            view.Verify();
            viewEngine.Verify();
        }
Ejemplo n.º 18
0
        public async Task ExecuteResultAsync_UsesCompositeViewEngineFromServices_IfViewEngineIsNotSpecified()          
        {
            // Arrange
            var viewName = "some-view-name";
            var context = new ActionContext(new DefaultHttpContext(),
                                            new RouteData(),
                                            new ActionDescriptor { Name = viewName });
            var viewEngine = new Mock<ICompositeViewEngine>();
            viewEngine.Setup(e => e.FindView(context, viewName))
                      .Returns(ViewEngineResult.Found(viewName, Mock.Of<IView>()))
                      .Verifiable();

            var serviceProvider = new Mock<IServiceProvider>();
            serviceProvider.Setup(p => p.GetService(typeof(ICompositeViewEngine)))
                           .Returns(viewEngine.Object);
            serviceProvider.Setup(p => p.GetService(typeof(ILogger<ViewResult>)))
                           .Returns(new Mock<ILogger<ViewResult>>().Object);
            serviceProvider.Setup(s => s.GetService(typeof(IOptions<MvcViewOptions>)))
                .Returns(() => {
                    var optionsAccessor = new Mock<IOptions<MvcViewOptions>>();
                    optionsAccessor.SetupGet(o => o.Options)
                        .Returns(new MvcViewOptions());
                    return optionsAccessor.Object;
                });
            context.HttpContext.RequestServices = serviceProvider.Object;

            var viewResult = new ViewResult
            {
                ViewName = viewName
            };

            // Act
            await viewResult.ExecuteResultAsync(context);

            // Assert
            viewEngine.Verify();
        }
Ejemplo n.º 19
0
        public async Task ExecuteResultAsync_UsesActionDescriptorName_IfViewNameIsNull()
        {
            // Arrange
            var viewName = "some-view-name";
            var context = new ActionContext(GetHttpContext(),
                                            new RouteData(),
                                            new ActionDescriptor { Name = viewName });
            var viewEngine = new Mock<ICompositeViewEngine>();
            viewEngine.Setup(e => e.FindView(context, viewName))
                      .Returns(ViewEngineResult.Found(viewName, Mock.Of<IView>()))
                      .Verifiable();

            var viewResult = new ViewResult
            {
                ViewEngine = viewEngine.Object
            };

            // Act
            await viewResult.ExecuteResultAsync(context);

            // Assert
            viewEngine.Verify();
        }
Ejemplo n.º 20
0
        public async Task ViewResult_NotifiesViewNotFound()
        {
            // Arrange
            var viewName = "myview";
            var httpContext = GetHttpContext();
            var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());

            var listener = new TestTelemetryListener();
            httpContext.RequestServices.GetRequiredService<TelemetryListener>().SubscribeWithAdapter(listener);

            var viewEngine = new Mock<IViewEngine>();
            var view = Mock.Of<IView>();

            viewEngine.Setup(e => e.FindView(context, "myview"))
                      .Returns(ViewEngineResult.NotFound("myview", new string[] { "location/myview" }));

            var viewResult = new ViewResult
            {
                ViewName = viewName,
                ViewEngine = viewEngine.Object,
                ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()),
                TempData = Mock.Of<ITempDataDictionary>(),
            };

            // Act
            await Assert.ThrowsAsync<InvalidOperationException>(
                async () => await viewResult.ExecuteResultAsync(context));

            // Assert
            Assert.NotNull(listener.ViewResultViewNotFound);
            Assert.NotNull(listener.ViewResultViewNotFound.ActionContext);
            Assert.NotNull(listener.ViewResultViewNotFound.Result);
            Assert.Equal(new string[] { "location/myview" }, listener.ViewResultViewNotFound.SearchedLocations);
            Assert.Equal("myview", listener.ViewResultViewNotFound.ViewName);
        }
Ejemplo n.º 21
0
        public async Task ViewResult_SetsStatusCode()
        {
            // Arrange
            var viewName = "myview";
            var httpContext = GetHttpContext();
            var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
            var viewEngine = new Mock<IViewEngine>();
            var view = Mock.Of<IView>();

            viewEngine.Setup(e => e.FindView(context, "myview"))
                      .Returns(ViewEngineResult.Found("myview", view));

            var viewResult = new ViewResult
            {
                ViewName = viewName,
                ViewEngine = viewEngine.Object,
                StatusCode = 404,
                ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()),
                TempData = Mock.Of<ITempDataDictionary>(),
            };

            // Act
            await viewResult.ExecuteResultAsync(context);

            // Assert
            Assert.Equal(404, httpContext.Response.StatusCode);
        }
Ejemplo n.º 22
0
        public async Task ViewResult_SetsContentTypeHeader(
            MediaTypeHeaderValue contentType,
            string expectedContentTypeHeaderValue)
        {
            // Arrange
            var viewName = "myview";
            var httpContext = GetHttpContext();
            var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
            var viewEngine = new Mock<IViewEngine>();
            var view = Mock.Of<IView>();
            var contentTypeBeforeViewResultExecution = contentType?.ToString();

            viewEngine.Setup(e => e.FindView(context, "myview"))
                      .Returns(ViewEngineResult.Found("myview", view));

            var viewResult = new ViewResult
            {
                ViewName = viewName,
                ViewEngine = viewEngine.Object,
                ContentType = contentType
            };

            // Act
            await viewResult.ExecuteResultAsync(context);

            // Assert
            Assert.Equal(expectedContentTypeHeaderValue, httpContext.Response.ContentType);

            // Check if the original instance provided by the user has not changed.
            // Since we do not have access to the new instance created within the view executor,
            // check if at least the content is the same.
            var contentTypeAfterViewResultExecution = contentType?.ToString();
            Assert.Equal(contentTypeBeforeViewResultExecution, contentTypeAfterViewResultExecution);
        }
Ejemplo n.º 23
0
        public async Task ViewResult_NotifiesViewFound()
        {
            // Arrange
            var viewName = "myview";
            var httpContext = GetHttpContext();
            var context = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());

            var listener = new TestNotificationListener();
            httpContext.RequestServices.GetRequiredService<INotifier>().EnlistTarget(listener);

            var viewEngine = new Mock<IViewEngine>();
            var view = Mock.Of<IView>();

            viewEngine.Setup(e => e.FindView(context, "myview"))
                      .Returns(ViewEngineResult.Found("myview", view));

            var viewResult = new ViewResult
            {
                ViewName = viewName,
                ViewEngine = viewEngine.Object,
            };

            // Act
            await viewResult.ExecuteResultAsync(context);

            // Assert
            Assert.NotNull(listener.ViewResultViewFound);
            Assert.NotNull(listener.ViewResultViewFound.ActionContext);
            Assert.NotNull(listener.ViewResultViewFound.Result);
            Assert.NotNull(listener.ViewResultViewFound.View);
            Assert.Equal("myview", listener.ViewResultViewFound.ViewName);
        }
Ejemplo n.º 24
0
        public async Task ExecuteResultAsync_UsesCompositeViewEngineFromServices_IfViewEngineIsNotSpecified()          
        {
            // Arrange
            var viewName = "some-view-name";
            var context = new ActionContext(new DefaultHttpContext(),
                                            new RouteData(),
                                            new ActionDescriptor { Name = viewName });
            var viewEngine = new Mock<ICompositeViewEngine>();
            viewEngine.Setup(e => e.FindView(context, viewName))
                      .Returns(ViewEngineResult.Found(viewName, Mock.Of<IView>()))
                      .Verifiable();

            var serviceProvider = new Mock<IServiceProvider>();

            var telemetry = new TelemetryListener("Microsoft.AspNet");
            serviceProvider
                .Setup(s => s.GetService(typeof(TelemetrySource)))
                .Returns(telemetry);
            serviceProvider
                .Setup(s => s.GetService(typeof(TelemetryListener)))
                .Returns(telemetry);
            serviceProvider.Setup(p => p.GetService(typeof(ICompositeViewEngine)))
                           .Returns(viewEngine.Object);
            serviceProvider.Setup(p => p.GetService(typeof(ILogger<ViewResult>)))
                           .Returns(new Mock<ILogger<ViewResult>>().Object);
            serviceProvider.Setup(s => s.GetService(typeof(IOptions<MvcViewOptions>)))
                .Returns(() => {
                    var optionsAccessor = new Mock<IOptions<MvcViewOptions>>();
                    optionsAccessor.SetupGet(o => o.Value)
                        .Returns(new MvcViewOptions());
                    return optionsAccessor.Object;
                });
            context.HttpContext.RequestServices = serviceProvider.Object;

            var viewResult = new ViewResult
            {
                ViewName = viewName,
                ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()),
                TempData = Mock.Of<ITempDataDictionary>(),
            };

            // Act
            await viewResult.ExecuteResultAsync(context);

            // Assert
            viewEngine.Verify();
        }