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 CreateController_ThrowsIfControllerCannotBeActivated(Type type) { // Arrange var actionDescriptor = new ControllerActionDescriptor { ControllerTypeInfo = type.GetTypeInfo() }; var context = new ControllerContext() { ActionDescriptor = actionDescriptor, HttpContext = new DefaultHttpContext() { RequestServices = GetServices(), }, }; var factory = new DefaultControllerActivator(new TypeActivatorCache()); // Act and Assert var exception = Assert.Throws <InvalidOperationException>(() => factory.Create(context)); Assert.Equal( $"The type '{type.FullName}' cannot be activated by '{typeof(DefaultControllerActivator).FullName}' " + "because it is either a value type, an interface, an abstract class or an open generic type.", exception.Message); }
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 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 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); }
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); }
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); }
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); }