public void CreateCachedController_UnderlyingFactoryReturnsNull()
        {
            // Arrange
            RequestContext requestContext = GetRequestContext();

            Mock <IControllerFactory> mockUnderlyingFactory = new Mock <IControllerFactory>();

            mockUnderlyingFactory.Expect(o => o.CreateController(requestContext, "home")).Returns((IController)null).AtMostOnce();
            MvcDynamicSessionControllerFactory factory = new MvcDynamicSessionControllerFactory(mockUnderlyingFactory.Object);

            // Act
            IController controller1 = factory.CreateCachedController(requestContext, "home");
            IController controller2 = factory.CreateController(requestContext, "home");

            // Assert
            Assert.IsNull(controller1);
            Assert.IsNull(controller2);
            mockUnderlyingFactory.Verify();
        }
        public void ReleaseCachedController_ControllerInstanceCached_ReleasesInstance()
        {
            // Arrange
            RequestContext  requestContext = GetRequestContext();
            EmptyController controller     = new EmptyController();

            Mock <IControllerFactory> mockUnderlyingFactory = new Mock <IControllerFactory>();

            mockUnderlyingFactory.Expect(o => o.CreateController(requestContext, "home")).Returns(controller).AtMostOnce();
            mockUnderlyingFactory.Expect(o => o.ReleaseController(controller)).Verifiable();
            MvcDynamicSessionControllerFactory factory = new MvcDynamicSessionControllerFactory(mockUnderlyingFactory.Object);

            // Act
            factory.CreateCachedController(requestContext, "home");
            factory.ReleaseCachedController(requestContext.HttpContext);

            // Assert
            mockUnderlyingFactory.Verify();
        }
        public void CreateController_RemovesCachedController()
        {
            // Arrange
            RequestContext  requestContext = GetRequestContext();
            EmptyController controller     = new EmptyController();
            int             numTimesCalled = 0;

            Mock <IControllerFactory> mockUnderlyingFactory = new Mock <IControllerFactory>();

            mockUnderlyingFactory.Expect(o => o.CreateController(requestContext, "home")).Returns(controller).Callback(() => { numTimesCalled++; });
            MvcDynamicSessionControllerFactory factory = new MvcDynamicSessionControllerFactory(mockUnderlyingFactory.Object);

            // Act & assert 1
            IController controller1 = factory.CreateCachedController(requestContext, "home");
            IController controller2 = factory.CreateController(requestContext, "home");

            Assert.AreEqual(1, numTimesCalled);

            // Act & assert 2
            IController controller3 = factory.CreateController(requestContext, "home");

            Assert.AreEqual(2, numTimesCalled);
        }