コード例 #1
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);
        }
コード例 #2
0
ファイル: ViewResultTest.cs プロジェクト: 4myBenefits/Mvc
        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);
        }