Beispiel #1
0
        public void ProcessRequestAsync_AsyncController_DisposesControllerOnException()
        {
            // Arrange
            Mock <IAsyncController> mockController = new Mock <IAsyncController>();

            mockController
            .Setup(
                o =>
                o.BeginExecute(
                    It.IsAny <RequestContext>(),
                    It.IsAny <AsyncCallback>(),
                    It.IsAny <object>()
                    )
                )
            .Throws(new Exception("Some exception text."));
            mockController.As <IDisposable>().Setup(o => o.Dispose()).Verifiable();

            MvcHandler handler = GetMvcHandler(mockController.Object);

            // Act & assert
            Assert.Throws <Exception>(
                delegate
            {
                handler.BeginProcessRequest(handler.RequestContext.HttpContext, null, null);
            },
                @"Some exception text."
                );

            mockController.Verify();
        }
        public void ProcessRequestAsync_SyncController_NormalExecution2()
        {
            // Arrange
            MyCustomerController controller = new MyCustomerController();

            MvcHandler handler = GetMvcHandler(controller);

            handler.RequestContext.RouteData.Values["action"] = "Widget";

            // Act
            IAsyncResult outerAsyncResult = handler.BeginProcessRequest(handler.RequestContext.HttpContext, null, null);

            handler.EndProcessRequest(outerAsyncResult);

            // Assert
            Assert.Equal(1, controller.Called);
        }
Beispiel #3
0
        public void ProcessRequestAsync_AsyncController_NormalExecution()
        {
            // Arrange
            using (MockAsyncResult innerAsyncResult = new MockAsyncResult())
            {
                bool disposeWasCalled = false;

                Mock <IAsyncController> mockController = new Mock <IAsyncController>();
                mockController
                .Setup(
                    o =>
                    o.BeginExecute(
                        It.IsAny <RequestContext>(),
                        It.IsAny <AsyncCallback>(),
                        It.IsAny <object>()
                        )
                    )
                .Returns(innerAsyncResult);
                mockController
                .As <IDisposable>()
                .Setup(o => o.Dispose())
                .Callback(
                    delegate
                {
                    disposeWasCalled = true;
                }
                    );

                MvcHandler handler = GetMvcHandler(mockController.Object);

                // Act & assert
                IAsyncResult outerAsyncResult = handler.BeginProcessRequest(
                    handler.RequestContext.HttpContext,
                    null,
                    null
                    );
                Assert.False(disposeWasCalled);

                handler.EndProcessRequest(outerAsyncResult);
                Assert.True(disposeWasCalled);
                mockController.Verify(o => o.EndExecute(innerAsyncResult), Times.AtMostOnce());
            }
        }
Beispiel #4
0
        public void ProcessRequestAsync_SyncController_NormalExecution()
        {
            // Arrange
            bool executeWasCalled = false;
            bool disposeWasCalled = false;

            Mock <IController> mockController = new Mock <IController>();

            mockController
            .Setup(o => o.Execute(It.IsAny <RequestContext>()))
            .Callback(
                delegate
            {
                executeWasCalled = true;
            }
                );
            mockController
            .As <IDisposable>()
            .Setup(o => o.Dispose())
            .Callback(
                delegate
            {
                disposeWasCalled = true;
            }
                );

            MvcHandler handler = GetMvcHandler(mockController.Object);

            // Act & assert
            IAsyncResult outerAsyncResult = handler.BeginProcessRequest(
                handler.RequestContext.HttpContext,
                null,
                null
                );

            Assert.False(executeWasCalled);
            Assert.False(disposeWasCalled);

            handler.EndProcessRequest(outerAsyncResult);
            Assert.True(executeWasCalled);
            Assert.True(disposeWasCalled);
        }
Beispiel #5
0
        public void ProcessRequestAsync_AsyncController_NormalExecution()
        {
            // Arrange
            MockAsyncResult innerAsyncResult = new MockAsyncResult();
            bool            disposeWasCalled = false;

            Mock <IAsyncController> mockController = new Mock <IAsyncController>();

            mockController.Expect(o => o.BeginExecute(It.IsAny <RequestContext>(), It.IsAny <AsyncCallback>(), It.IsAny <object>())).Returns(innerAsyncResult);
            mockController.Expect(o => o.EndExecute(innerAsyncResult)).AtMostOnce();
            mockController.As <IDisposable>().Expect(o => o.Dispose()).Callback(delegate { disposeWasCalled = true; });

            MvcHandler handler = GetMvcHandler(mockController.Object);

            // Act & assert
            IAsyncResult outerAsyncResult = handler.BeginProcessRequest(handler.RequestContext.HttpContext, null, null);

            Assert.IsFalse(disposeWasCalled, "Dispose shouldn't have been called yet.");

            handler.EndProcessRequest(outerAsyncResult);
            Assert.IsTrue(disposeWasCalled);
        }