Example #1
0
        public void ExecuteThrowsIfCalledTwice()
        {
            // Arrange
            EmptyControllerBase controller     = new EmptyControllerBase();
            RequestContext      requestContext = new RequestContext(HttpContextHelpers.GetMockHttpContext().Object, new RouteData());

            // Act
            ((IController)controller).Execute(requestContext); // first call
            ExceptionHelper.ExpectInvalidOperationException(
                delegate {
                ((IController)controller).Execute(requestContext);     // second call
            },
                @"A single instance of controller 'System.Web.Mvc.Test.ControllerBaseTest+EmptyControllerBase' cannot be used to handle multiple requests. If a custom controller factory is in use, make sure that it creates a new instance of the controller for each request.");

            // Assert
            Assert.AreEqual(1, controller.NumTimesExecuteCoreCalled);
        }
        public void ExecuteCallsControllerBaseExecute()
        {
            // Arrange
            RequestContext requestContext = new RequestContext(HttpContextHelpers.GetMockHttpContext().Object, new RouteData());

            Mock <ControllerBaseHelper> mockController = new Mock <ControllerBaseHelper>()
            {
                CallBase = true
            };

            mockController.Setup(c => c.PublicInitialize(requestContext)).Verifiable();
            mockController.Setup(c => c.PublicExecuteCore()).Verifiable();
            IController controller = mockController.Object;

            // Act
            controller.Execute(requestContext);

            // Assert
            mockController.Verify();
        }