/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

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