Exemplo n.º 1
0
        public void Create_TypeActivatesTypesWithServices()
        {
            // Arrange
            var activator = new DefaultControllerActivator(new TypeActivatorCache());
            var serviceProvider = new Mock<IServiceProvider>(MockBehavior.Strict);
            var testService = new TestService();
            serviceProvider.Setup(s => s.GetService(typeof(TestService)))
                           .Returns(testService)
                           .Verifiable();

            var httpContext = new DefaultHttpContext
            {
                RequestServices = serviceProvider.Object
            };

            var context = new ControllerContext(
                new ActionContext(
                    httpContext,
                    new RouteData(),
                    new ControllerActionDescriptor
                    {
                        ControllerTypeInfo = typeof(TypeDerivingFromControllerWithServices).GetTypeInfo()
                    }));

            // Act
            var instance = activator.Create(context);

            // Assert
            var controller = Assert.IsType<TypeDerivingFromControllerWithServices>(instance);
            Assert.Same(testService, controller.TestService);
            serviceProvider.Verify();
        }
Exemplo n.º 2
0
    public void CreateActivator_ActivatesControllerInstance()
    {
        // Arrange
        var expected          = new TestService();
        var activator         = new DefaultControllerActivator(Mock.Of <ITypeActivatorCache>());
        var activatorProvider = new ControllerActivatorProvider(activator);
        var descriptor        = new ControllerActionDescriptor
        {
            ControllerTypeInfo = typeof(TestController).GetTypeInfo(),
        };
        var serviceProvider = new ServiceCollection()
                              .AddSingleton(expected)
                              .BuildServiceProvider();
        var context = new ControllerContext
        {
            HttpContext = new DefaultHttpContext
            {
                RequestServices = serviceProvider,
            },
        };

        // Act
        var activatorDelegate = activatorProvider.CreateActivator(descriptor);
        var result            = activatorDelegate(context);

        // Assert
        var actual = Assert.IsType <TestController>(result);

        Assert.Same(expected, actual.TestService);
    }
        public void Activate_PopulatesServicesFromServiceContainer()
        {
            // Arrange
            var urlHelper = Mock.Of <IUrlHelper>();
            var service   = new Mock <IServiceProvider>();

            service.Setup(s => s.GetService(typeof(IUrlHelper)))
            .Returns(urlHelper);

            var httpContext = new Mock <HttpContext>();

            httpContext.SetupGet(c => c.RequestServices)
            .Returns(service.Object);
            var routeContext = new RouteContext(httpContext.Object);
            var controller   = new TestController();
            var context      = new ActionContext(routeContext, new ActionDescriptor())
            {
                Controller = controller
            };
            var activator = new DefaultControllerActivator();

            // Act
            activator.Activate(controller, context);

            // Assert
            Assert.Same(urlHelper, controller.Helper);
        }
Exemplo n.º 4
0
        public void Create_CreatesInstancesOfTypes(Type type)
        {
            // Arrange
            var activator = new DefaultControllerActivator(new TypeActivatorCache());
            var serviceProvider = new Mock<IServiceProvider>(MockBehavior.Strict);

            var httpContext = new DefaultHttpContext
            {
                RequestServices = serviceProvider.Object
            };

            var context = new ControllerContext(
                new ActionContext(
                    httpContext,
                    new RouteData(),
                    new ControllerActionDescriptor
                    {
                        ControllerTypeInfo = type.GetTypeInfo()
                    }));

            // Act
            var instance = activator.Create(context);

            // Assert
            Assert.IsType(type, instance);
        }
        public void Activate_SetsPropertiesFromActionContextHierarchy()
        {
            // Arrange
            var httpRequest = Mock.Of <HttpRequest>();
            var httpContext = new Mock <HttpContext>();

            httpContext.SetupGet(c => c.Request)
            .Returns(httpRequest);
            httpContext.SetupGet(c => c.RequestServices)
            .Returns(Mock.Of <IServiceProvider>());
            var routeContext = new RouteContext(httpContext.Object);
            var controller   = new TestController();
            var context      = new ActionContext(routeContext, new ActionDescriptor())
            {
                Controller = controller
            };
            var activator = new DefaultControllerActivator();

            // Act
            activator.Activate(controller, context);

            // Assert
            Assert.Same(context, controller.ActionContext);
            Assert.Same(httpContext.Object, controller.HttpContext);
            Assert.Same(httpRequest, controller.GetHttpRequest());
        }
Exemplo n.º 6
0
        public void Activate_SetsPropertiesFromActionContextHierarchy()
        {
            // Arrange
            var httpRequest = Mock.Of<HttpRequest>();
            var httpContext = new Mock<HttpContext>();
            httpContext.SetupGet(c => c.Request)
                       .Returns(httpRequest);
            httpContext.SetupGet(c => c.RequestServices)
                       .Returns(Mock.Of<IServiceProvider>());
            var routeContext = new RouteContext(httpContext.Object);
            var controller = new TestController();
            var context = new ActionContext(routeContext, new ActionDescriptor())
            {
                Controller = controller
            };
            var activator = new DefaultControllerActivator();

            // Act
            activator.Activate(controller, context);

            // Assert
            Assert.Same(context, controller.ActionContext);
            Assert.Same(httpContext.Object, controller.HttpContext);
            Assert.Same(httpRequest, controller.GetHttpRequest());
        }
Exemplo n.º 7
0
    public void Create_TypeActivatesTypesWithServices()
    {
        // Arrange
        var activator       = new DefaultControllerActivator(new TypeActivatorCache());
        var serviceProvider = new Mock <IServiceProvider>(MockBehavior.Strict);
        var testService     = new TestService();

        serviceProvider.Setup(s => s.GetService(typeof(TestService)))
        .Returns(testService)
        .Verifiable();

        var httpContext = new DefaultHttpContext
        {
            RequestServices = serviceProvider.Object
        };

        var context = new ControllerContext(
            new ActionContext(
                httpContext,
                new RouteData(),
                new ControllerActionDescriptor
        {
            ControllerTypeInfo = typeof(TypeDerivingFromControllerWithServices).GetTypeInfo()
        }));

        // Act
        var instance = activator.Create(context);

        // Assert
        var controller = Assert.IsType <TypeDerivingFromControllerWithServices>(instance);

        Assert.Same(testService, controller.TestService);
        serviceProvider.Verify();
    }
        public void Activate_IgnoresPropertiesThatAreNotDecoratedWithActivateAttribute()
        {
            // Arrange
            var services = new Mock <IServiceProvider>();

            services.Setup(s => s.GetService(typeof(IUrlHelper)))
            .Returns(Mock.Of <IUrlHelper>());
            services.Setup(s => s.GetService(typeof(IModelMetadataProvider)))
            .Returns(new EmptyModelMetadataProvider());

            var httpContext = new Mock <HttpContext>();

            httpContext.SetupGet(c => c.Response)
            .Returns(Mock.Of <HttpResponse>());
            httpContext.SetupGet(c => c.RequestServices)
            .Returns(services.Object);
            var routeContext = new RouteContext(httpContext.Object);
            var controller   = new TestController();
            var context      = new ActionContext(routeContext, new ActionDescriptor())
            {
                Controller = controller
            };
            var activator = new DefaultControllerActivator();

            // Act
            activator.Activate(controller, context);

            // Assert
            Assert.Null(controller.Response);
        }
Exemplo n.º 9
0
        public void Activate_SetsBindingContext()
        {
            // Arrange
            var bindingContext = new ActionBindingContext();

            var services = GetServices();

            services.GetRequiredService <IScopedInstance <ActionBindingContext> >().Value = bindingContext;

            var httpContext = new Mock <HttpContext>();

            httpContext.SetupGet(c => c.RequestServices)
            .Returns(services);
            var routeContext = new RouteContext(httpContext.Object);

            var controller = new TestController();
            var context    = new ActionContext(routeContext, new ActionDescriptor());

            var activator = new DefaultControllerActivator();

            // Act
            activator.Activate(controller, context);

            // Assert
            Assert.Same(bindingContext, controller.BindingContext);
        }
Exemplo n.º 10
0
    public void Create_CreatesInstancesOfTypes(Type type)
    {
        // Arrange
        var activator       = new DefaultControllerActivator(new TypeActivatorCache());
        var serviceProvider = new Mock <IServiceProvider>(MockBehavior.Strict);

        var httpContext = new DefaultHttpContext
        {
            RequestServices = serviceProvider.Object
        };

        var context = new ControllerContext(
            new ActionContext(
                httpContext,
                new RouteData(),
                new ControllerActionDescriptor
        {
            ControllerTypeInfo = type.GetTypeInfo()
        }));

        // Act
        var instance = activator.Create(context);

        // Assert
        Assert.IsType(type, instance);
    }
Exemplo n.º 11
0
        public void Activate_SetsViewDatDictionary()
        {
            // Arrange
            var service = new Mock <IServiceProvider>();

            service.Setup(s => s.GetService(typeof(IModelMetadataProvider)))
            .Returns(Mock.Of <IModelMetadataProvider>());

            var httpContext = new Mock <HttpContext>();

            httpContext.SetupGet(c => c.RequestServices)
            .Returns(service.Object);
            var routeContext = new RouteContext(httpContext.Object);
            var controller   = new TestController();
            var context      = new ActionContext(routeContext, new ActionDescriptor())
            {
                Controller = controller
            };
            var activator = new DefaultControllerActivator();

            // Act
            activator.Activate(controller, context);

            // Assert
            Assert.NotNull(controller.GetViewData());
        }
Exemplo n.º 12
0
    public void DefaultControllerActivator_ReleasesNonIDisposableController()
    {
        // Arrange
        var activator  = new DefaultControllerActivator(Mock.Of <ITypeActivatorCache>());
        var controller = new object();

        // Act + Assert (does not throw)
        activator.Release(Mock.Of <ControllerContext>(), controller);
    }
Exemplo n.º 13
0
    public async Task ReleaseAsync_SynchronouslyDisposesController_IfDisposableAsync()
    {
        // Arrange
        var controller = new MyController();
        var activator  = new DefaultControllerActivator(Mock.Of <ITypeActivatorCache>());

        // Act
        await activator.ReleaseAsync(new ControllerContext(), controller);

        // Assert
        Assert.True(controller.Disposed);
    }
Exemplo n.º 14
0
    public void Release_DisposesController_IfDisposable()
    {
        // Arrange
        var controller = new MyController();
        var activator  = new DefaultControllerActivator(Mock.Of <ITypeActivatorCache>());

        // Act
        activator.Release(new ControllerContext(), controller);

        // Assert
        Assert.True(controller.Disposed);
    }
Exemplo n.º 15
0
    public void CreateReleaser_ReturnsNullIfControllerIsNotDisposable()
    {
        // Arrange
        var activator         = new DefaultControllerActivator(Mock.Of <ITypeActivatorCache>());
        var activatorProvider = new ControllerActivatorProvider(activator);
        var descriptor        = new ControllerActionDescriptor
        {
            ControllerTypeInfo = typeof(TestController).GetTypeInfo(),
        };

        // Act
        var releaseDelegate = activatorProvider.CreateReleaser(descriptor);

        // Assert
        Assert.Null(releaseDelegate);
    }
Exemplo n.º 16
0
    public void CreateReleaser_ReturnsDelegateThatDisposesInstance()
    {
        // Arrange
        var activator         = new DefaultControllerActivator(Mock.Of <ITypeActivatorCache>());
        var activatorProvider = new ControllerActivatorProvider(activator);
        var descriptor        = new ControllerActionDescriptor
        {
            ControllerTypeInfo = typeof(DisposableController).GetTypeInfo(),
        };
        var controller = new DisposableController();

        // Act
        var releaseDelegate = activatorProvider.CreateReleaser(descriptor);

        // Assert
        Assert.NotNull(releaseDelegate);
        releaseDelegate(new ControllerContext(), controller);
        Assert.True(controller.Disposed);
    }
Exemplo n.º 17
0
        public void Activate_SetsViewDatDictionary()
        {
            // Arrange
            var services = GetServices();

            var httpContext = new Mock <HttpContext>();

            httpContext.SetupGet(c => c.RequestServices)
            .Returns(services);
            var routeContext = new RouteContext(httpContext.Object);
            var controller   = new TestController();
            var context      = new ActionContext(routeContext, new ActionDescriptor());
            var activator    = new DefaultControllerActivator();

            // Act
            activator.Activate(controller, context);

            // Assert
            Assert.NotNull(controller.GetViewData());
        }
Exemplo n.º 18
0
        public void Activate_PopulatesServicesFromServiceContainer()
        {
            // Arrange
            var services  = GetServices();
            var urlHelper = services.GetRequiredService <IUrlHelper>();

            var httpContext = new Mock <HttpContext>();

            httpContext.SetupGet(c => c.RequestServices)
            .Returns(services);
            var routeContext = new RouteContext(httpContext.Object);
            var controller   = new TestController();
            var context      = new ActionContext(routeContext, new ActionDescriptor());
            var activator    = new DefaultControllerActivator();

            // Act
            activator.Activate(controller, context);

            // Assert
            Assert.Same(urlHelper, controller.Helper);
        }
Exemplo n.º 19
0
        public void Activate_IgnoresPropertiesThatAreNotDecoratedWithActivateAttribute()
        {
            // Arrange
            var services = GetServices();

            var httpContext = new Mock <HttpContext>();

            httpContext.SetupGet(c => c.Response)
            .Returns(Mock.Of <HttpResponse>());
            httpContext.SetupGet(c => c.RequestServices)
            .Returns(services);
            var routeContext = new RouteContext(httpContext.Object);
            var controller   = new TestController();
            var context      = new ActionContext(routeContext, new ActionDescriptor());
            var activator    = new DefaultControllerActivator();

            // Act
            activator.Activate(controller, context);

            // Assert
            Assert.Null(controller.Response);
        }
Exemplo n.º 20
0
        public void Activate_SetsViewDatDictionary()
        {
            // Arrange
            var service = new Mock<IServiceProvider>();
            service.Setup(s => s.GetService(typeof(IModelMetadataProvider)))
                   .Returns(Mock.Of<IModelMetadataProvider>());

            var httpContext = new Mock<HttpContext>();
            httpContext.SetupGet(c => c.RequestServices)
                       .Returns(service.Object);
            var routeContext = new RouteContext(httpContext.Object);
            var controller = new TestController();
            var context = new ActionContext(routeContext, new ActionDescriptor())
            {
                Controller = controller
            };
            var activator = new DefaultControllerActivator();

            // Act
            activator.Activate(controller, context);

            // Assert
            Assert.NotNull(controller.GetViewData());
        }
Exemplo n.º 21
0
 public ControllerActivator(IConfigurationRoot config)
 {
     Configuration      = config;
     TypeActivatorCache = new TypeActivatorCache();
     DefaultActivator   = new DefaultControllerActivator(TypeActivatorCache);
 }
Exemplo n.º 22
0
 public ControllerActivator(IConfigurationRoot configuration)
 {
     typeActivatorCache = new TypeActivatorCache();
     defaultActivator   = new DefaultControllerActivator(typeActivatorCache);
     this.configuration = configuration;
 }
Exemplo n.º 23
0
 public void Setup()
 {
     _container = new Mock <IComponentProvider>(MockBehavior.Strict);
     _activator = new DefaultControllerActivator(_container.Object);
 }
Exemplo n.º 24
0
        public void Activate_IgnoresPropertiesThatAreNotDecoratedWithActivateAttribute()
        {
            // Arrange
            var httpContext = new Mock<HttpContext>();
            httpContext.SetupGet(c => c.Response)
                       .Returns(Mock.Of<HttpResponse>());
            httpContext.SetupGet(c => c.RequestServices)
                       .Returns(Mock.Of<IServiceProvider>());
            var routeContext = new RouteContext(httpContext.Object);
            var controller = new TestController();
            var context = new ActionContext(routeContext, new ActionDescriptor())
            {
                Controller = controller
            };
            var activator = new DefaultControllerActivator();

            // Act
            activator.Activate(controller, context);

            // Assert
            Assert.Null(controller.Response);
        }
Exemplo n.º 25
0
        public void Activate_PopulatesServicesFromServiceContainer()
        {
            // Arrange
            var urlHelper = Mock.Of<IUrlHelper>();
            var service = new Mock<IServiceProvider>();
            service.Setup(s => s.GetService(typeof(IUrlHelper)))
                   .Returns(urlHelper);

            var httpContext = new Mock<HttpContext>();
            httpContext.SetupGet(c => c.RequestServices)
                       .Returns(service.Object);
            var routeContext = new RouteContext(httpContext.Object);
            var controller = new TestController();
            var context = new ActionContext(routeContext, new ActionDescriptor())
            {
                Controller = controller
            };
            var activator = new DefaultControllerActivator();

            // Act
            activator.Activate(controller, context);

            // Assert
            Assert.Same(urlHelper, controller.Helper);
        }
Exemplo n.º 26
0
 public void Teardown()
 {
     _container = null;
     _activator = null;
 }
Exemplo n.º 27
0
        public void Release_DisposesController_IfDisposable()
        {
            // Arrange
            var controller = new MyController();
            var activator = new DefaultControllerActivator(Mock.Of<ITypeActivatorCache>());

            // Act
            activator.Release(new ControllerContext(), controller);

            // Assert
            Assert.Equal(true, controller.Disposed);
        }
Exemplo n.º 28
0
        public void DefaultControllerActivator_ReleasesNonIDisposableController()
        {
            // Arrange
            var activator = new DefaultControllerActivator(Mock.Of<ITypeActivatorCache>());
            var controller = new object();

            // Act + Assert (does not throw)
            activator.Release(Mock.Of<ControllerContext>(), controller);
        }