Ejemplo n.º 1
0
        public void InstantiateComponent_ThrowsForNullInstance()
        {
            // Arrange
            var componentType = typeof(EmptyComponent);
            var factory = new ComponentFactory(new NullResultComponentActivator());

            // Act
            var ex = Assert.Throws<InvalidOperationException>(() => factory.InstantiateComponent(GetServiceProvider(), componentType));
            Assert.Equal($"The component activator returned a null value for a component of type {componentType.FullName}.", ex.Message);
        }
Ejemplo n.º 2
0
        public void InstantiateComponent_CreatesInstance_NonComponent()
        {
            // Arrange
            var componentType = typeof(List<string>);
            var factory = new ComponentFactory(new DefaultComponentActivator());

            // Assert
            var ex = Assert.Throws<ArgumentException>(() => factory.InstantiateComponent(GetServiceProvider(), componentType));
            Assert.StartsWith($"The type {componentType.FullName} does not implement {nameof(IComponent)}.", ex.Message);
        }
Ejemplo n.º 3
0
        public void InstantiateComponent_CreatesInstance()
        {
            // Arrange
            var componentType = typeof(EmptyComponent);
            var factory       = new ComponentFactory();

            // Act
            var instance = factory.InstantiateComponent(GetServiceProvider(), componentType);

            // Assert
            Assert.NotNull(instance);
            Assert.IsType <EmptyComponent>(instance);
        }
Ejemplo n.º 4
0
        public void InstantiateComponent_IgnoresPropertiesWithoutInjectAttribute()
        {
            // Arrange
            var componentType = typeof(ComponentWithNonInjectableProperties);
            var factory = new ComponentFactory(new DefaultComponentActivator());

            // Act
            var instance = factory.InstantiateComponent(GetServiceProvider(), componentType);

            // Assert
            Assert.NotNull(instance);
            var component = Assert.IsType<ComponentWithNonInjectableProperties>(instance);
            // Public, and non-public properties, and properties with non-public setters should get assigned
            Assert.NotNull(component.Property1);
            Assert.Null(component.Property2);
        }
Ejemplo n.º 5
0
        public void InstantiateComponent_AssignsPropertiesWithInjectAttribute()
        {
            // Arrange
            var componentType = typeof(ComponentWithInjectProperties);
            var factory       = new ComponentFactory();

            // Act
            var instance = factory.InstantiateComponent(GetServiceProvider(), componentType);

            // Assert
            Assert.NotNull(instance);
            var component = Assert.IsType <ComponentWithInjectProperties>(instance);

            // Public, and non-public properties, and properties with non-public setters should get assigned
            Assert.NotNull(component.Property1);
            Assert.NotNull(component.GetProperty2());
            Assert.NotNull(component.Property3);
            Assert.NotNull(component.Property4);
        }
Ejemplo n.º 6
0
        public void InstantiateComponent_CreatesInstance_WithCustomActivator()
        {
            // Arrange
            var componentType = typeof(EmptyComponent);
            var factory = new ComponentFactory(new CustomComponentActivator<ComponentWithInjectProperties>());

            // Act
            var instance = factory.InstantiateComponent(GetServiceProvider(), componentType);

            // Assert
            Assert.NotNull(instance);
            var component = Assert.IsType<ComponentWithInjectProperties>(instance); // Custom activator returns a different type

            // Public, and non-public properties, and properties with non-public setters should get assigned
            Assert.NotNull(component.Property1);
            Assert.NotNull(component.GetProperty2());
            Assert.NotNull(component.Property3);
            Assert.NotNull(component.Property4);
        }
Ejemplo n.º 7
0
        public void InstantiateComponent_AssignsPropertiesWithInjectAttributeOnBaseType()
        {
            // Arrange
            var componentType = typeof(DerivedComponent);
            var factory = new ComponentFactory(new CustomComponentActivator<DerivedComponent>());

            // Act
            var instance = factory.InstantiateComponent(GetServiceProvider(), componentType);

            // Assert
            Assert.NotNull(instance);
            var component = Assert.IsType<DerivedComponent>(instance);
            Assert.NotNull(component.Property1);
            Assert.NotNull(component.GetProperty2());
            Assert.NotNull(component.Property3);

            // Property on derived type without [Inject] should not be assigned
            Assert.Null(component.Property4);
            // Property on the base type with the [Inject] attribute should
            Assert.NotNull(((ComponentWithInjectProperties)component).Property4);
        }