Example #1
0
        public void InvokeAction_ThrowsOnActionExecutingException_NotHandledAsync()
        {
            var actionLog    = new List <string>();
            var actionFilter = new AsyncActionFilterImpl()
            {
                OnActionExecutingImpl = filterContext =>
                {
                    actionLog.Add("OnActionExecuting1");
                },
                OnActionExecutedImpl = filterContext =>
                {
                    actionLog.Add("OnActionExecuted1");
                }
            };

            GlobalFilters.Filters.Add(actionFilter);
            var controllerContext = GetControllerContext();

            AssertEx.Throws <Exception>(() =>
            {
                var retVal = InvokeAction(controllerContext, nameof(TestController.ActionThrowsExceptionAndIsNotHandled), null, null);
            }, "Some exception text.");

            Assert.Equal(new[] {
                "OnActionExecuting1",
                "OnActionExecuted1"
            }, actionLog.ToArray());
        }
Example #2
0
        public void InvokeAction_ThrowsOnActionExecutingException_HandledAsync()
        {
            var actionLog    = new List <string>();
            var actionFilter = new AsyncActionFilterImpl()
            {
                OnActionExecutingImpl = filterContext =>
                {
                    actionLog.Add("OnActionExecuting");
                },
                OnActionExecutedImpl = filterContext =>
                {
                    actionLog.Add("OnActionExecuted");
                    Assert.Equal("Some exception text.", filterContext.Exception.Message);
                    filterContext.ExceptionHandled = true;
                    filterContext.Result           = new LoggingActionResult("Handled Exception");
                }
            };

            GlobalFilters.Filters.Add(actionFilter);
            var controllerContext = GetControllerContext();

            var retVal = InvokeAction(controllerContext, nameof(TestController.ActionThrowsExceptionAndIsNotHandled), null, null);

            Assert.True(retVal);
            Assert.Equal(new[] {
                "OnActionExecuting",
                "OnActionExecuted"
            }, actionLog.ToArray());
            Assert.Equal("Handled Exception", ((TestController)controllerContext.Controller).Log);
        }