public void ErrorControllerHelper_HandleUnknownAction_WithNoViews_SearchesForViewTwiceThenDisplaysError()
        {
            var viewEngineMock = new Mock<IViewEngine>();
            viewEngineMock
                .Setup(m => m.FindView(It.IsAny<ControllerContext>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
                .Returns(new ViewEngineResult(Enumerable.Empty<string>()));
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(viewEngineMock.Object);

            using (var httpOut = new FakeHttpOutput())
            {
                var httpContext = new FakeHttpContext(new FakeHttpRequest(), new FakeHttpResponse(httpOut));
                var controllerContext = CreateControllerContext(httpContext);

                ErrorControllerHelper.HandleUnknownAction(controllerContext, "action");

                viewEngineMock
                    .Verify(m => m.FindView(It.IsAny<ControllerContext>(), It.IsAny<string>(), It.IsAny<string>(), true), Times.Exactly(2));
                viewEngineMock
                    .Verify(m => m.FindView(It.IsAny<ControllerContext>(), It.IsAny<string>(), It.IsAny<string>(), false), Times.Exactly(2));

                Assert.That(httpContext.Response.ContentType, Is.EqualTo("text/html"));
                Assert.That(httpOut.GetContentString(), Does.StartWith("<!DOCTYPE html>"));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FakeHttpServerUtility"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        public FakeHttpServerUtility(FakeHttpContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            this.context = context;
        }
        public void ErrorControllerHelper_HandleUnknownAction_WithFoundView_ExecutesResult()
        {
            var viewMock = new Mock<IView>();

            var viewEngineMock = new Mock<IViewEngine>();
            viewEngineMock
                .Setup(m => m.FindView(It.IsAny<ControllerContext>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
                .Returns(new ViewEngineResult(viewMock.Object, viewEngineMock.Object));
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(viewEngineMock.Object);

            var httpContext = new FakeHttpContext();
            var routeData = new RouteData();
            routeData.Values["action"] = "action";
            var controllerContext = CreateControllerContext(httpContext, routeData);

            ErrorControllerHelper.HandleUnknownAction(controllerContext, "action");

            viewEngineMock.Verify(m => m.FindView(It.IsAny<ControllerContext>(), "action", It.IsAny<string>(), true), Times.Once);
            viewMock.Verify(m => m.Render(It.IsAny<ViewContext>(), httpContext.Response.Output), Times.Once);

            Assert.That(httpContext.Response.StatusCode, Is.EqualTo(500));
            Assert.That(httpContext.Response.TrySkipIisCustomErrors, Is.True);
        }