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 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);
        }
        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);
        }
        public void CreateReleaser_InvokesIControllerActivator_IfItIsNotDefaultControllerActivator()
        {
            // Arrange
            var expected  = new object();
            var activator = new Mock <IControllerActivator>();

            activator.Setup(a => a.Release(It.IsAny <ControllerContext>(), expected))
            .Verifiable();
            var activatorProvider = new ControllerActivatorProvider(activator.Object);
            var descriptor        = new ControllerActionDescriptor
            {
                ControllerTypeInfo = typeof(object).GetTypeInfo(),
            };

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

            releaseDelegate(new ControllerContext(), expected);

            // Assert
            activator.Verify();
        }