public void CreateController_ThrowsIfRequestContextIsNull()
        {
            // Arrange
            MvcDynamicSessionControllerFactory factory = new MvcDynamicSessionControllerFactory();

            // Act & assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                factory.CreateController(null, "home");
            }, "requestContext");
        }
        public void ReleaseController_ForwardsToUnderlyingFactory()
        {
            // Arrange
            EmptyController controller = new EmptyController();

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

            mockUnderlyingFactory.Expect(o => o.ReleaseController(controller)).Verifiable();
            MvcDynamicSessionControllerFactory factory = new MvcDynamicSessionControllerFactory(mockUnderlyingFactory.Object);

            // Act
            factory.ReleaseController(controller);

            // Assert
            mockUnderlyingFactory.Verify();
        }
        public void ReleaseCachedController_ControllerInstanceNotCached_DoesNothing()
        {
            // Arrange
            RequestContext requestContext = GetRequestContext();

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

            mockUnderlyingFactory.Expect(o => o.ReleaseController(It.IsAny <IController>())).Never();
            MvcDynamicSessionControllerFactory factory = new MvcDynamicSessionControllerFactory(mockUnderlyingFactory.Object);

            // Act
            factory.ReleaseCachedController(requestContext.HttpContext);

            // Assert
            // If we got this far, success!
        }
예제 #4
0
        public static void Initialize()
        {
            var builder = new ContainerBuilder();

            builder.RegisterControllers(Assembly.GetExecutingAssembly());

            var host = new MultiGameHost();

            builder.RegisterInstance(host)
            .As <MultiGameHost>();

            _container = builder.Build();

            var factory =
                new MvcDynamicSessionControllerFactory(new AutofacControllerFactory(new ContainerProvider(_container)));

            ControllerBuilder.Current.SetControllerFactory(factory);
        }
        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);
        }