public void OnExceptionWithNullFilterContextThrows() { // Arrange HandleErrorAttribute attr = new HandleErrorAttribute(); // Act & Assert Assert.ThrowsArgumentNull( delegate { attr.OnException(null /* filterContext */); }, "filterContext"); }
public void ErrorHandlingDoesNotFireIfCalledInChildAction() { // Arrange HandleErrorAttribute attr = new HandleErrorAttribute(); Mock<ExceptionContext> context = new Mock<ExceptionContext>(); context.Setup(c => c.IsChildAction).Returns(true); // Act attr.OnException(context.Object); // Assert Assert.IsType<EmptyResult>(context.Object.Result); }
public void ErrorHandlingDoesNotFireIfCalledInChildAction() { // Arrange HandleErrorAttribute attr = new HandleErrorAttribute(); Mock <ExceptionContext> context = new Mock <ExceptionContext>(); context.Setup(c => c.IsChildAction).Returns(true); // Act attr.OnException(context.Object); // Assert Assert.IsType <EmptyResult>(context.Object.Result); }
public void OnException() { // Arrange HandleErrorAttribute attr = new HandleErrorAttribute() { View = "SomeView", Master = "SomeMaster", ExceptionType = typeof(ArgumentException) }; Exception exception = new ArgumentNullException(); Mock <HttpContextBase> mockHttpContext = new Mock <HttpContextBase>(MockBehavior.Strict); mockHttpContext.Setup(c => c.IsCustomErrorEnabled).Returns(true); mockHttpContext.Setup(c => c.Session).Returns((HttpSessionStateBase)null); mockHttpContext.Setup(c => c.Response.Clear()).Verifiable(); mockHttpContext.SetupSet(c => c.Response.StatusCode = 500).Verifiable(); mockHttpContext.SetupSet(c => c.Response.TrySkipIisCustomErrors = true).Verifiable(); TempDataDictionary tempData = new TempDataDictionary(); IViewEngine viewEngine = new Mock <IViewEngine>().Object; Controller controller = new Mock <Controller>().Object; controller.TempData = tempData; ExceptionContext context = GetExceptionContext( mockHttpContext.Object, controller, exception ); // Exception attr.OnException(context); // Assert mockHttpContext.Verify(); ViewResult viewResult = context.Result as ViewResult; Assert.NotNull(viewResult); Assert.Equal(tempData, viewResult.TempData); Assert.Equal("SomeView", viewResult.ViewName); Assert.Equal("SomeMaster", viewResult.MasterName); HandleErrorInfo viewData = viewResult.ViewData.Model as HandleErrorInfo; Assert.NotNull(viewData); Assert.Same(exception, viewData.Exception); Assert.Equal("SomeController", viewData.ControllerName); Assert.Equal("SomeAction", viewData.ActionName); }
public void OnExceptionWithNon500ExceptionDoesNothing() { // Arrange HandleErrorAttribute attr = new HandleErrorAttribute(); ActionResult result = new EmptyResult(); ExceptionContext context = GetExceptionContext(GetHttpContext(), null, new HttpException(404, "Some Exception")); context.Result = result; // Exception attr.OnException(context); // Assert Assert.Same(result, context.Result); }
public void OnExceptionWithCustomErrorsDisabledDoesNothing() { // Arrange HandleErrorAttribute attr = new HandleErrorAttribute(); ActionResult result = new EmptyResult(); ExceptionContext context = GetExceptionContext(GetHttpContext(false), null, new Exception()); context.Result = result; // Exception attr.OnException(context); // Assert Assert.Same(result, context.Result); }
public void OnExceptionWithNon500ExceptionDoesNothing() { // Arrange HandleErrorAttribute attr = new HandleErrorAttribute(); ActionResult result = new EmptyResult(); ExceptionContext context = GetExceptionContext(GetHttpContext(), null, new HttpException(404, "Some Exception")); context.Result = result; // Exception attr.OnException(context); // Assert Assert.AreSame(result, context.Result, "The context's Result property should have remain unchanged."); }
public void OnExceptionWithCustomErrorsDisabledDoesNothing() { // Arrange HandleErrorAttribute attr = new HandleErrorAttribute(); ActionResult result = new EmptyResult(); ExceptionContext context = GetExceptionContext(GetHttpContext(false), null, new Exception()); context.Result = result; // Exception attr.OnException(context); // Assert Assert.AreSame(result, context.Result, "The context's Result property should have remain unchanged."); }
public void OnExceptionWithWrongExceptionTypeDoesNothing() { // Arrange HandleErrorAttribute attr = new HandleErrorAttribute() { ExceptionType = typeof(ArgumentException) }; ActionResult result = new EmptyResult(); ExceptionContext context = GetExceptionContext(GetHttpContext(), null, new InvalidCastException()); context.Result = result; // Exception attr.OnException(context); // Assert Assert.Same(result, context.Result); }
public void OnException() { // Arrange HandleErrorAttribute attr = new HandleErrorAttribute() { View = "SomeView", Master = "SomeMaster", ExceptionType = typeof(ArgumentException) }; Exception exception = new ArgumentNullException(); Mock<HttpContextBase> mockHttpContext = new Mock<HttpContextBase>(MockBehavior.Strict); mockHttpContext.Expect(c => c.IsCustomErrorEnabled).Returns(true); mockHttpContext.Expect(c => c.Session).Returns((HttpSessionStateBase)null); mockHttpContext.Expect(c => c.Response.Clear()).Verifiable(); mockHttpContext.ExpectSet(c => c.Response.StatusCode, 500).Verifiable(); mockHttpContext.ExpectSet(c => c.Response.TrySkipIisCustomErrors, true).Verifiable(); TempDataDictionary tempData = new TempDataDictionary(); IViewEngine viewEngine = new Mock<IViewEngine>().Object; Controller controller = new Mock<Controller>().Object; controller.TempData = tempData; ExceptionContext context = GetExceptionContext(mockHttpContext.Object, controller, exception); // Exception attr.OnException(context); // Assert mockHttpContext.Verify(); ViewResult viewResult = context.Result as ViewResult; Assert.IsNotNull(viewResult, "The Result property should have been set to an instance of ViewResult."); Assert.AreEqual(tempData, viewResult.TempData); Assert.AreEqual("SomeView", viewResult.ViewName); Assert.AreEqual("SomeMaster", viewResult.MasterName); HandleErrorInfo viewData = viewResult.ViewData.Model as HandleErrorInfo; Assert.IsNotNull(viewData, "The ViewData model should have been set to an instance of ExceptionViewData."); Assert.AreSame(exception, viewData.Exception); Assert.AreEqual("SomeController", viewData.ControllerName); Assert.AreEqual("SomeAction", viewData.ActionName); }
public void OnExceptionWithWrongExceptionTypeDoesNothing() { // Arrange HandleErrorAttribute attr = new HandleErrorAttribute() { ExceptionType = typeof(ArgumentException) }; ActionResult result = new EmptyResult(); ExceptionContext context = GetExceptionContext(GetHttpContext(), null, new InvalidCastException()); context.Result = result; // Exception attr.OnException(context); // Assert Assert.AreSame(result, context.Result, "The context's Result property should have remain unchanged."); }
public void OnExceptionWithNullFilterContextThrows() { // Arrange HandleErrorAttribute attr = new HandleErrorAttribute(); // Act & Assert ExceptionHelper.ExpectArgumentNullException( delegate { attr.OnException(null /* filterContext */); }, "filterContext"); }
public void OnExceptionWithExceptionHandledDoesNothing() { // Arrange HandleErrorAttribute attr = new HandleErrorAttribute(); ActionResult result = new EmptyResult(); ExceptionContext context = GetExceptionContext(GetHttpContext(), null, new Exception()); context.Result = result; context.ExceptionHandled = true; // Exception attr.OnException(context); // Assert Assert.Same(result, context.Result); }