public void ActivatorResolverAndDependencyResolverAreNeverCalledWhenControllerActivatorIsPassedInConstructor()
        {
            // Arrange
            var context            = new RequestContext();
            var expectedController = new Goodcontroller();

            Mock <IControllerActivator> activator = new Mock <IControllerActivator>();

            activator
            .Setup(a => a.Create(context, typeof(Goodcontroller)))
            .Returns(expectedController);

            var resolverActivator = new Mock <IControllerActivator>(MockBehavior.Strict);
            var activatorResolver = new Resolver <IControllerActivator>
            {
                Current = resolverActivator.Object
            };

            var dependencyResolver = new Mock <IDependencyResolver>(MockBehavior.Strict);

            //Act
            var factory = new DefaultControllerFactory(
                activator.Object,
                activatorResolver,
                dependencyResolver.Object
                );
            IController returnedController = factory.GetControllerInstance(
                context,
                typeof(Goodcontroller)
                );

            //Assert
            Assert.Same(returnedController, expectedController);
        }
        public void GetControllerDelegatesToActivatorResolver()
        {
            //Arrange
            var context            = new RequestContext();
            var expectedController = new Goodcontroller();
            var resolverActivator  = new Mock <IControllerActivator>();

            resolverActivator
            .Setup(a => a.Create(context, typeof(Goodcontroller)))
            .Returns(expectedController);
            var activatorResolver = new Resolver <IControllerActivator>
            {
                Current = resolverActivator.Object
            };
            var factory = new DefaultControllerFactory(null, activatorResolver, null);

            //Act
            IController returnedController = factory.GetControllerInstance(
                context,
                typeof(Goodcontroller)
                );

            //Assert
            Assert.Same(returnedController, expectedController);
        }
예제 #3
0
        public void GetControllerInstanceThrowsIfControllerTypeIsNotControllerBase()
        {
            // Arrange
            RequestContext           requestContext = new RequestContext(new Mock <HttpContextBase>().Object, new RouteData());
            DefaultControllerFactory factory        = new DefaultControllerFactory();

            // Act
            Assert.Throws <ArgumentException>(
                delegate { factory.GetControllerInstance(requestContext, typeof(int)); },
                "The controller type 'System.Int32' must implement IController.\r\nParameter name: controllerType");
        }
        public void GetControllerInstanceThrowsIfControllerTypeIsNotControllerBase()
        {
            // Arrange
            DefaultControllerFactory factory = new DefaultControllerFactory();

            // Act
            ExceptionHelper.ExpectArgumentException(
                delegate {
                factory.GetControllerInstance(typeof(int));
            },
                "The controller type 'System.Int32' must implement IController.\r\nParameter name: controllerType");
        }
예제 #5
0
        public void GetControllerDelegatesToActivatorCreateInstanceWhenDependencyResolverReturnsNull()
        {
            // Arrange
            var context            = new RequestContext();
            var dependencyResolver = new Mock <IDependencyResolver>();
            var factory            = new DefaultControllerFactory(null, null, dependencyResolver.Object);

            // Act & Assert
            Assert.Throws <InvalidOperationException>(
                () => factory.GetControllerInstance(context, typeof(NoParameterlessCtor)),
                "An error occurred when trying to create a controller of type 'System.Web.Mvc.Test.DefaultControllerFactoryTest+NoParameterlessCtor'. Make sure that the controller has a parameterless public constructor."
                );
        }
예제 #6
0
        public void GetControllerInstanceConsultsSetControllerActivator()
        {
            //Arrange
            Mock <IControllerActivator> activator = new Mock <IControllerActivator>();
            DefaultControllerFactory    factory   = new DefaultControllerFactory(activator.Object);
            RequestContext context = new RequestContext();

            //Act
            factory.GetControllerInstance(context, typeof(Goodcontroller));

            //Assert
            activator.Verify(l => l.Create(context, typeof(Goodcontroller)));
        }
예제 #7
0
        public void GetControllerDelegatesToDependencyResolveWhenActivatorResolverIsNull()
        {
            // Arrange
            var context            = new RequestContext();
            var expectedController = new Goodcontroller();
            var dependencyResolver = new Mock <IDependencyResolver>(MockBehavior.Strict);

            dependencyResolver.Setup(dr => dr.GetService(typeof(Goodcontroller))).Returns(expectedController);
            var factory = new DefaultControllerFactory(null, null, dependencyResolver.Object);

            // Act
            IController returnedController = factory.GetControllerInstance(context, typeof(Goodcontroller));

            // Assert
            Assert.Same(returnedController, expectedController);
        }
        public void ActivatorResolverAndDependencyResolverAreNeverCalledWhenControllerActivatorIsPassedInConstructor()
        {
            // Arrange
            var context = new RequestContext();
            var expectedController = new Goodcontroller();

            Mock<IControllerActivator> activator = new Mock<IControllerActivator>();
            activator.Setup(a => a.Create(context, typeof(Goodcontroller))).Returns(expectedController);

            var resolverActivator = new Mock<IControllerActivator>(MockBehavior.Strict);
            var activatorResolver = new Resolver<IControllerActivator> { Current = resolverActivator.Object };

            var dependencyResolver = new Mock<IDependencyResolver>(MockBehavior.Strict);

            //Act
            var factory = new DefaultControllerFactory(activator.Object, activatorResolver, dependencyResolver.Object);
            IController returnedController = factory.GetControllerInstance(context, typeof(Goodcontroller));

            //Assert
            Assert.Same(returnedController, expectedController);
        }
        public void GetControllerDelegatesToActivatorCreateInstanceWhenDependencyResolverReturnsNull()
        {
            // Arrange
            var context = new RequestContext();
            var dependencyResolver = new Mock<IDependencyResolver>();
            var factory = new DefaultControllerFactory(null, null, dependencyResolver.Object);

            // Act & Assert
            Assert.Throws<InvalidOperationException>(
                () => factory.GetControllerInstance(context, typeof(NoParameterlessCtor)),
                "An error occurred when trying to create a controller of type 'System.Web.Mvc.Test.DefaultControllerFactoryTest+NoParameterlessCtor'. Make sure that the controller has a parameterless public constructor."
                );
        }
        public void GetControllerDelegatesToDependencyResolveWhenActivatorResolverIsNull()
        {
            // Arrange
            var context = new RequestContext();
            var expectedController = new Goodcontroller();
            var dependencyResolver = new Mock<IDependencyResolver>(MockBehavior.Strict);
            dependencyResolver.Setup(dr => dr.GetService(typeof(Goodcontroller))).Returns(expectedController);
            var factory = new DefaultControllerFactory(null, null, dependencyResolver.Object);

            // Act
            IController returnedController = factory.GetControllerInstance(context, typeof(Goodcontroller));

            // Assert
            Assert.Same(returnedController, expectedController);
        }
        public void GetControllerDelegatesToActivatorResolver()
        {
            //Arrange
            var context = new RequestContext();
            var expectedController = new Goodcontroller();
            var resolverActivator = new Mock<IControllerActivator>();
            resolverActivator.Setup(a => a.Create(context, typeof(Goodcontroller))).Returns(expectedController);
            var activatorResolver = new Resolver<IControllerActivator> { Current = resolverActivator.Object };
            var factory = new DefaultControllerFactory(null, activatorResolver, null);

            //Act
            IController returnedController = factory.GetControllerInstance(context, typeof(Goodcontroller));

            //Assert
            Assert.Same(returnedController, expectedController);
        }
        public void GetControllerInstanceConsultsSetControllerActivator()
        {
            //Arrange
            Mock<IControllerActivator> activator = new Mock<IControllerActivator>();
            DefaultControllerFactory factory = new DefaultControllerFactory(activator.Object);
            RequestContext context = new RequestContext();

            //Act
            factory.GetControllerInstance(context, typeof(Goodcontroller));

            //Assert
            activator.Verify(l => l.Create(context, typeof(Goodcontroller)));
        }
        public void GetControllerInstanceThrowsIfControllerTypeIsNotControllerBase()
        {
            // Arrange
            RequestContext requestContext = new RequestContext(new Mock<HttpContextBase>().Object, new RouteData());
            DefaultControllerFactory factory = new DefaultControllerFactory();

            // Act
            Assert.Throws<ArgumentException>(
                delegate { factory.GetControllerInstance(requestContext, typeof(int)); },
                "The controller type 'System.Int32' must implement IController.\r\nParameter name: controllerType");
        }