Ejemplo n.º 1
0
        private ActionResult BeingInvokeActionMethodWithFiltersTester(Func <IAsyncResult> beginFunction, Func <ActionResult> endFunction, bool checkBegin, bool checkEnd, IActionFilter[] filters)
        {
            AsyncControllerActionInvoker invoker              = new AsyncControllerActionInvoker();
            ControllerContext            controllerContext    = new ControllerContext();
            Dictionary <string, object>  parameters           = new Dictionary <string, object>();
            Mock <AsyncActionDescriptor> mockActionDescriptor = new Mock <AsyncActionDescriptor>();
            bool endExecuteCalled          = false;
            bool beginExecuteCalled        = false;
            Func <ActionResult> endExecute = () =>
            {
                endExecuteCalled = true;
                return(endFunction());
            };
            Func <IAsyncResult> beingExecute = () =>
            {
                beginExecuteCalled = true;
                return(beginFunction());
            };

            mockActionDescriptor.Setup(d => d.BeginExecute(controllerContext, parameters, It.IsAny <AsyncCallback>(), It.IsAny <object>())).Returns(beingExecute);
            mockActionDescriptor.Setup(d => d.EndExecute(It.IsAny <IAsyncResult>())).Returns(endExecute);

            IAsyncResult outerAsyncResult = null;

            try
            {
                outerAsyncResult = invoker.BeginInvokeActionMethodWithFilters(controllerContext, filters, mockActionDescriptor.Object, parameters, null, null);
            }
            catch (Exception ex)
            {
                if (checkEnd)
                {
                    // Testing end, so not expecting exception thrown from begin
                    Assert.NotNull(ex);
                }
                else
                {
                    throw ex;
                }
            }

            Assert.NotNull(outerAsyncResult);
            Assert.Equal(checkBegin, beginExecuteCalled);
            Assert.False(endExecuteCalled);

            ActionExecutedContext postContext = invoker.EndInvokeActionMethodWithFilters(outerAsyncResult);

            Assert.NotNull(postContext);
            if (checkEnd)
            {
                Assert.True(endExecuteCalled);
            }
            return(postContext.Result);
        }
        public void InvokeActionMethodWithFilters_ShortCircuited()
        {
            // Arrange
            List <string>               actionLog         = new List <string>();
            ControllerContext           controllerContext = new ControllerContext();
            Dictionary <string, object> parameters        = new Dictionary <string, object>();
            ActionResult actionResult = new ViewResult();

            ActionFilterImpl filter1 = new ActionFilterImpl()
            {
                OnActionExecutingImpl = delegate(ActionExecutingContext filterContext) {
                    actionLog.Add("OnActionExecuting1");
                },
                OnActionExecutedImpl = delegate(ActionExecutedContext filterContext) {
                    actionLog.Add("OnActionExecuted1");
                }
            };
            ActionFilterImpl filter2 = new ActionFilterImpl()
            {
                OnActionExecutingImpl = delegate(ActionExecutingContext filterContext) {
                    actionLog.Add("OnActionExecuting2");
                    filterContext.Result = actionResult;
                },
                OnActionExecutedImpl = delegate(ActionExecutedContext filterContext) {
                    actionLog.Add("OnActionExecuted2");
                }
            };

            Mock <AsyncActionDescriptor> mockActionDescriptor = new Mock <AsyncActionDescriptor>();

            mockActionDescriptor.Expect(d => d.BeginExecute(controllerContext, parameters, It.IsAny <AsyncCallback>(), It.IsAny <object>())).Throws(new Exception("I shouldn't have been called."));
            mockActionDescriptor.Expect(d => d.EndExecute(It.IsAny <IAsyncResult>())).Throws(new Exception("I shouldn't have been called."));

            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            IActionFilter[] filters = new IActionFilter[] { filter1, filter2 };

            // Act
            IAsyncResult          outerAsyncResult = invoker.BeginInvokeActionMethodWithFilters(controllerContext, filters, mockActionDescriptor.Object, parameters, null, null);
            ActionExecutedContext postContext      = invoker.EndInvokeActionMethodWithFilters(outerAsyncResult);

            // Assert
            CollectionAssert.AreEqual(
                new string[] { "OnActionExecuting1", "OnActionExecuting2", "OnActionExecuted1" },
                actionLog);
            Assert.AreEqual(actionResult, postContext.Result);
        }
        public void InvokeActionMethodWithFilters()
        {
            // Arrange
            List <string>               actionLog         = new List <string>();
            ControllerContext           controllerContext = new ControllerContext();
            Dictionary <string, object> parameters        = new Dictionary <string, object>();
            MockAsyncResult             innerAsyncResult  = new MockAsyncResult();
            ActionResult actionResult = new ViewResult();

            ActionFilterImpl filter1 = new ActionFilterImpl()
            {
                OnActionExecutingImpl = delegate(ActionExecutingContext filterContext) { actionLog.Add("OnActionExecuting1"); },
                OnActionExecutedImpl  = delegate(ActionExecutedContext filterContext) { actionLog.Add("OnActionExecuted1"); }
            };
            ActionFilterImpl filter2 = new ActionFilterImpl()
            {
                OnActionExecutingImpl = delegate(ActionExecutingContext filterContext) { actionLog.Add("OnActionExecuting2"); },
                OnActionExecutedImpl  = delegate(ActionExecutedContext filterContext) { actionLog.Add("OnActionExecuted2"); }
            };

            Mock <AsyncActionDescriptor> mockActionDescriptor = new Mock <AsyncActionDescriptor>();

            mockActionDescriptor.Setup(d => d.BeginExecute(controllerContext, parameters, It.IsAny <AsyncCallback>(), It.IsAny <object>())).Returns(innerAsyncResult);
            mockActionDescriptor.Setup(d => d.EndExecute(innerAsyncResult)).Returns(actionResult);

            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            IActionFilter[] filters = new IActionFilter[] { filter1, filter2 };

            // Act
            IAsyncResult          outerAsyncResult = invoker.BeginInvokeActionMethodWithFilters(controllerContext, filters, mockActionDescriptor.Object, parameters, null, null);
            ActionExecutedContext postContext      = invoker.EndInvokeActionMethodWithFilters(outerAsyncResult);

            // Assert
            Assert.Equal(new[] { "OnActionExecuting1", "OnActionExecuting2", "OnActionExecuted2", "OnActionExecuted1" }, actionLog.ToArray());
            Assert.Equal(actionResult, postContext.Result);
        }
        private ActionResult BeingInvokeActionMethodWithFiltersTester(Func<IAsyncResult> beginFunction, Func<ActionResult> endFunction, bool checkBegin, bool checkEnd, IActionFilter[] filters)
        {
            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();
            ControllerContext controllerContext = new ControllerContext();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            Mock<AsyncActionDescriptor> mockActionDescriptor = new Mock<AsyncActionDescriptor>();
            bool endExecuteCalled = false;
            bool beginExecuteCalled = false;
            Func<ActionResult> endExecute = () =>
            {
                endExecuteCalled = true;
                return endFunction();
            };
            Func<IAsyncResult> beingExecute = () =>
            {
                beginExecuteCalled = true;
                return beginFunction();
            };

            mockActionDescriptor.Setup(d => d.BeginExecute(controllerContext, parameters, It.IsAny<AsyncCallback>(), It.IsAny<object>())).Returns(beingExecute);
            mockActionDescriptor.Setup(d => d.EndExecute(It.IsAny<IAsyncResult>())).Returns(endExecute);

            IAsyncResult outerAsyncResult = null;
            try
            {
                outerAsyncResult = invoker.BeginInvokeActionMethodWithFilters(controllerContext, filters, mockActionDescriptor.Object, parameters, null, null);
            }
            catch (Exception ex)
            {
                if (checkEnd)
                {
                    // Testing end, so not expecting exception thrown from begin
                    Assert.NotNull(ex);
                }
                else
                {
                    throw ex;
                }
            }

            Assert.NotNull(outerAsyncResult);
            Assert.Equal(checkBegin, beginExecuteCalled);
            Assert.False(endExecuteCalled);

            ActionExecutedContext postContext = invoker.EndInvokeActionMethodWithFilters(outerAsyncResult);

            Assert.NotNull(postContext);
            if (checkEnd)
            {
                Assert.True(endExecuteCalled);
            }
            return postContext.Result;
        }
        public void InvokeActionMethodWithFilters_ShortCircuited() {
            // Arrange
            List<string> actionLog = new List<string>();
            ControllerContext controllerContext = new ControllerContext();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            ActionResult actionResult = new ViewResult();

            ActionFilterImpl filter1 = new ActionFilterImpl() {
                OnActionExecutingImpl = delegate(ActionExecutingContext filterContext) {
                    actionLog.Add("OnActionExecuting1");
                },
                OnActionExecutedImpl = delegate(ActionExecutedContext filterContext) {
                    actionLog.Add("OnActionExecuted1");
                }
            };
            ActionFilterImpl filter2 = new ActionFilterImpl() {
                OnActionExecutingImpl = delegate(ActionExecutingContext filterContext) {
                    actionLog.Add("OnActionExecuting2");
                    filterContext.Result = actionResult;
                },
                OnActionExecutedImpl = delegate(ActionExecutedContext filterContext) {
                    actionLog.Add("OnActionExecuted2");
                }
            };

            Mock<AsyncActionDescriptor> mockActionDescriptor = new Mock<AsyncActionDescriptor>();
            mockActionDescriptor.Expect(d => d.BeginExecute(controllerContext, parameters, It.IsAny<AsyncCallback>(), It.IsAny<object>())).Throws(new Exception("I shouldn't have been called."));
            mockActionDescriptor.Expect(d => d.EndExecute(It.IsAny<IAsyncResult>())).Throws(new Exception("I shouldn't have been called."));

            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();
            IActionFilter[] filters = new IActionFilter[] { filter1, filter2 };

            // Act
            IAsyncResult outerAsyncResult = invoker.BeginInvokeActionMethodWithFilters(controllerContext, filters, mockActionDescriptor.Object, parameters, null, null);
            ActionExecutedContext postContext = invoker.EndInvokeActionMethodWithFilters(outerAsyncResult);

            // Assert
            CollectionAssert.AreEqual(
                new string[] { "OnActionExecuting1", "OnActionExecuting2", "OnActionExecuted1" },
                actionLog);
            Assert.AreEqual(actionResult, postContext.Result);
        }
        public void InvokeActionMethodWithFilters()
        {
            // Arrange
            List<string> actionLog = new List<string>();
            ControllerContext controllerContext = new ControllerContext();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            MockAsyncResult innerAsyncResult = new MockAsyncResult();
            ActionResult actionResult = new ViewResult();

            ActionFilterImpl filter1 = new ActionFilterImpl()
            {
                OnActionExecutingImpl = delegate(ActionExecutingContext filterContext) { actionLog.Add("OnActionExecuting1"); },
                OnActionExecutedImpl = delegate(ActionExecutedContext filterContext) { actionLog.Add("OnActionExecuted1"); }
            };
            ActionFilterImpl filter2 = new ActionFilterImpl()
            {
                OnActionExecutingImpl = delegate(ActionExecutingContext filterContext) { actionLog.Add("OnActionExecuting2"); },
                OnActionExecutedImpl = delegate(ActionExecutedContext filterContext) { actionLog.Add("OnActionExecuted2"); }
            };

            Mock<AsyncActionDescriptor> mockActionDescriptor = new Mock<AsyncActionDescriptor>();
            mockActionDescriptor.Setup(d => d.BeginExecute(controllerContext, parameters, It.IsAny<AsyncCallback>(), It.IsAny<object>())).Returns(innerAsyncResult);
            mockActionDescriptor.Setup(d => d.EndExecute(innerAsyncResult)).Returns(actionResult);

            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();
            IActionFilter[] filters = new IActionFilter[] { filter1, filter2 };

            // Act
            IAsyncResult outerAsyncResult = invoker.BeginInvokeActionMethodWithFilters(controllerContext, filters, mockActionDescriptor.Object, parameters, null, null);
            ActionExecutedContext postContext = invoker.EndInvokeActionMethodWithFilters(outerAsyncResult);

            // Assert
            Assert.Equal(new[] { "OnActionExecuting1", "OnActionExecuting2", "OnActionExecuted2", "OnActionExecuted1" }, actionLog.ToArray());
            Assert.Equal(actionResult, postContext.Result);
        }