public void GetAllInstances_TypeDecorated3_ReturnsCollectionWithDecorators() { // Arrange var container = ContainerFactory.New(); var expectedSingletonHandler = new RealCommandHandler(); // Use the RegisterAll(Type, IEnumerable) overload. container.RegisterCollection(typeof(ICommandHandler<RealCommand>), new[] { expectedSingletonHandler }); container.RegisterDecorator(typeof(ICommandHandler<>), typeof(RealCommandHandlerDecorator)); // Act var handlers = container.GetAllInstances<ICommandHandler<RealCommand>>(); var decorator = handlers.Single(); // Assert AssertThat.IsInstanceOfType(typeof(RealCommandHandlerDecorator), decorator); Assert.IsTrue(object.ReferenceEquals(expectedSingletonHandler, ((RealCommandHandlerDecorator)decorator).Decorated)); }
public void GetAllInstances_CollectionManuallyRegisteredAndFuncDecoraterRegistered4_ThrowsExpectedException() { // Arrange var container = ContainerFactory.New(); var expectedSingletonHandler = new RealCommandHandler(); // Use the Register<T>(Func<T>) method. This is a strange (but legal) way of registering a service, // but will not work with a Func-Decorator. container.Register<IEnumerable<ICommandHandler<RealCommand>>>(() => new[] { expectedSingletonHandler }); container.RegisterDecorator(typeof(ICommandHandler<>), typeof(AsyncCommandHandlerProxy<>)); try { // Act container.GetAllInstances<ICommandHandler<RealCommand>>(); // Assert Assert.Fail("Exception expected."); } catch (ActivationException ex) { Assert_ExceptionContainsInfoAboutManualCollectionRegistrationMixedDecoratorsThatTakeAFunc(ex); } }
public void GetAllInstances_TypeDecoratedWithFuncDecorator2_InjectsADelegateThatCanCreateThatInstance() { // Arrange var container = ContainerFactory.New(); var expectedSingletonHandler = new RealCommandHandler(); // Use the RegisterAll<T>(T[]) overload. container.RegisterCollection<ICommandHandler<RealCommand>>(expectedSingletonHandler); container.RegisterDecorator(typeof(ICommandHandler<>), typeof(AsyncCommandHandlerProxy<>)); // Act var handlers = container.GetAllInstances<ICommandHandler<RealCommand>>(); var decorator = (AsyncCommandHandlerProxy<RealCommand>)handlers.Single(); var handler = decorator.DecorateeFactory(); // Assert AssertThat.IsInstanceOfType(typeof(RealCommandHandler), handler); }
public void GetAllInstances_CollectionManuallyRegisteredAndFuncDecoraterRegistered2_ThrowsExpectedException() { // Arrange var container = ContainerFactory.New(); var expectedSingletonHandler = new RealCommandHandler(); // Use the RegisterSingle<IEnumerable<T>> method container.RegisterSingleton<IEnumerable<ICommandHandler<RealCommand>>>(new[] { expectedSingletonHandler }); container.RegisterDecorator(typeof(ICommandHandler<>), typeof(AsyncCommandHandlerProxy<>)); try { // Act container.GetAllInstances<ICommandHandler<RealCommand>>(); // Assert Assert.Fail("Exception expected."); } catch (ActivationException ex) { Assert_ExceptionContainsInfoAboutManualCollectionRegistrationMixedDecoratorsThatTakeAFunc(ex); } }
public void GetAllInstances_TypeDecorated4_ReturnsCollectionWithDecorators() { // Arrange var container = ContainerFactory.New(); var expectedSingletonHandler = new RealCommandHandler(); // Use the RegisterSingle<IEnumerable<T>> method. container.RegisterSingleton<IEnumerable<ICommandHandler<RealCommand>>>(new[] { expectedSingletonHandler }); container.RegisterDecorator(typeof(ICommandHandler<>), typeof(RealCommandHandlerDecorator)); // Act var handlers = container.GetAllInstances<ICommandHandler<RealCommand>>(); var decorator = handlers.Single(); // Assert AssertThat.IsInstanceOfType(typeof(RealCommandHandlerDecorator), decorator); var decoratedInstance = ((RealCommandHandlerDecorator)decorator).Decorated; Assert.IsNotNull(decoratedInstance); Assert.IsTrue(object.ReferenceEquals(expectedSingletonHandler, decoratedInstance), "Not the same instance. Decorated instance is instance of type: " + decoratedInstance.GetType().Name); }