PageRenderTime 183ms CodeModel.GetById 80ms app.highlight 8ms RepoModel.GetById 92ms app.codeStats 1ms

/src/UnitTests/IOC/PropertyInjectionTests.cs

http://github.com/philiplaureano/LinFu
C# | 149 lines | 99 code | 34 blank | 16 comment | 0 complexity | 088b59f69fcecc9fb31aab4230eb65d2 MD5 | raw file
  1using System;
  2using System.Linq;
  3using System.Reflection;
  4using LinFu.IoC;
  5using LinFu.IoC.Configuration.Interfaces;
  6using Moq;
  7using Xunit;
  8using SampleLibrary;
  9using SampleLibrary.IOC;
 10
 11namespace LinFu.UnitTests.IOC
 12{
 13    public class PropertyInjectionTests : BaseTestFixture
 14    {
 15        [Fact]
 16        public void ShouldAutoInjectClassCreatedWithAutoCreate()
 17        {
 18            // Configure the container
 19            var container = new ServiceContainer();
 20            container.LoadFromBaseDirectory("*.dll");
 21
 22            var sampleService = new Mock<ISampleService>();
 23            container.AddService(sampleService.Object);
 24
 25            var instance =
 26                (SampleClassWithInjectionProperties) container.AutoCreate(typeof(SampleClassWithInjectionProperties));
 27
 28            // The container should initialize the SomeProperty method to match the mock ISampleService instance
 29            Assert.NotNull(instance.SomeProperty);
 30            Assert.Same(instance.SomeProperty, sampleService.Object);
 31        }
 32
 33        [Fact]
 34        public void ShouldAutoInjectProperty()
 35        {
 36            var container = new ServiceContainer();
 37            container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "LinFu*.dll");
 38
 39            var instance = new SampleClassWithInjectionProperties();
 40
 41            // Initialize the container
 42            container.Inject<ISampleService>().Using<SampleClass>().OncePerRequest();
 43            container.Inject<ISampleService>("MyService").Using(c => instance).OncePerRequest();
 44
 45            var result = container.GetService<ISampleService>("MyService");
 46            Assert.Same(result, instance);
 47
 48            // On initialization, the instance.SomeProperty value
 49            // should be a SampleClass type
 50            Assert.NotNull(instance.SomeProperty);
 51            Assert.Equal(typeof(SampleClass), instance.SomeProperty?.GetType());
 52        }
 53
 54        [Fact]
 55        public void ShouldAutoInjectPropertyWithoutCustomAttribute()
 56        {
 57            var container = new ServiceContainer();
 58            container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "LinFu*.dll");
 59
 60            var instance = new SampleClassWithUnmarkedInjectionProperties();
 61
 62            // Initialize the container with the dummy service
 63            container.Inject<ISampleService>().Using<SampleClass>().OncePerRequest();
 64            container.Inject<ISampleService>("MyService").Using(c => instance).OncePerRequest();
 65
 66            // Enable automatic property injection for every property
 67            container.SetCustomPropertyInjectionAttribute(null);
 68
 69            // Get the service instance
 70            var result = container.GetService<ISampleService>("MyService");
 71            Assert.Same(result, instance);
 72
 73            // Ensure that the injection occurred
 74            Assert.NotNull(instance.SomeProperty);
 75            Assert.Equal(typeof(SampleClass), instance.SomeProperty?.GetType());
 76        }
 77
 78        [Fact]
 79        public void ShouldAutoInjectServiceListIntoArrayDependency()
 80        {
 81            var container = new ServiceContainer();
 82            container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "LinFu*.dll");
 83
 84            var instance = new SampleClassWithArrayPropertyDependency();
 85
 86            // Initialize the container
 87            container.Inject<ISampleService>().Using<SampleClass>().OncePerRequest();
 88            container.Inject<SampleClassWithArrayPropertyDependency>().Using(c => instance).OncePerRequest();
 89
 90            var result = container.GetService<SampleClassWithArrayPropertyDependency>();
 91
 92            Assert.NotNull(result);
 93            Assert.NotNull(result.Property);
 94
 95            var serviceCount = result.Property.Count();
 96            Assert.True(serviceCount > 0);
 97        }
 98
 99        [Fact]
100        public void ShouldDetermineWhichPropertiesShouldBeInjected()
101        {
102            var targetType = typeof(SampleClassWithInjectionProperties);
103            var targetProperty = targetType.GetProperty("SomeProperty");
104            Assert.NotNull(targetProperty);
105
106            // Load the property injection filter by default
107            var container = new ServiceContainer();
108            container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "LinFu*.dll");
109
110            var filter = container.GetService<IMemberInjectionFilter<PropertyInfo>>();
111
112            Assert.NotNull(filter);
113
114            // The filter should return the targetProperty
115            var properties = filter.GetInjectableMembers(targetType).ToArray();
116            Assert.True(properties.Any());
117
118            var result = properties.First();
119            Assert.Equal(targetProperty, result);
120        }
121
122        [Fact]
123        public void ShouldSetPropertyValue()
124        {
125            var targetType = typeof(SampleClassWithInjectionProperties);
126            var targetProperty = targetType.GetProperty("SomeProperty");
127            Assert.NotNull(targetProperty);
128
129            // Configure the target
130            var instance = new SampleClassWithInjectionProperties();
131
132            // This is the service that should be assigned
133            // to the SomeProperty property
134            object service = new SampleClass();
135
136            // Initialize the container
137            var container = new ServiceContainer();
138            container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "LinFu*.dll");
139
140            var setter = container.GetService<IPropertySetter>();
141            Assert.NotNull(setter);
142
143            setter.Set(instance, targetProperty, service);
144
145            Assert.NotNull(instance.SomeProperty);
146            Assert.Same(service, instance.SomeProperty);
147        }
148    }
149}