33
44using System ;
55using System . Collections . Generic ;
6- using System . Text ;
76using System . Threading . Tasks ;
87using Microsoft . Extensions . DependencyInjection ;
98using Xunit ;
@@ -17,7 +16,7 @@ public void InstantiateComponent_CreatesInstance()
1716 {
1817 // Arrange
1918 var componentType = typeof ( EmptyComponent ) ;
20- var factory = new ComponentFactory ( ) ;
19+ var factory = new ComponentFactory ( new DefaultComponentActivator ( ) ) ;
2120
2221 // Act
2322 var instance = factory . InstantiateComponent ( GetServiceProvider ( ) , componentType ) ;
@@ -28,57 +27,56 @@ public void InstantiateComponent_CreatesInstance()
2827 }
2928
3029 [ Fact ]
31- public void InstantiateComponent_CreatesInstance_WithActivator ( )
30+ public void InstantiateComponent_CreatesInstance_NonComponent ( )
3231 {
3332 // Arrange
34- var componentType = typeof ( EmptyComponent ) ;
35- var factory = new ComponentFactory ( ) ;
36-
37- // Act
38- var instance = factory . InstantiateComponent ( GetServiceProviderWithActivator ( ) , componentType ) ;
39-
40- // Assert
41- Assert . NotNull ( instance ) ;
42- Assert . IsType < EmptyComponent > ( instance ) ;
43- }
44-
45- [ Fact ]
46- public void InstantiateComponent_CreatesInstance_WithActivator_NonComponent ( )
47- {
48- // Arrange
49- var componentType = typeof ( NonComponent ) ;
50- var factory = new ComponentFactory ( ) ;
33+ var componentType = typeof ( List < string > ) ;
34+ var factory = new ComponentFactory ( new DefaultComponentActivator ( ) ) ;
5135
5236 // Assert
53- Assert . Throws < ArgumentException > ( ( ) => factory . InstantiateComponent ( GetServiceProviderWithActivator ( ) , componentType ) ) ;
37+ var ex = Assert . Throws < ArgumentException > ( ( ) => factory . InstantiateComponent ( GetServiceProvider ( ) , componentType ) ) ;
38+ Assert . StartsWith ( $ "The type { componentType . FullName } does not implement { nameof ( IComponent ) } .", ex . Message ) ;
5439 }
5540
5641 [ Fact ]
57- public void InstantiateComponent_AssignsPropertiesWithInjectAttribute ( )
42+ public void InstantiateComponent_CreatesInstance_WithCustomActivator ( )
5843 {
5944 // Arrange
60- var componentType = typeof ( ComponentWithInjectProperties ) ;
61- var factory = new ComponentFactory ( ) ;
45+ var componentType = typeof ( EmptyComponent ) ;
46+ var factory = new ComponentFactory ( new CustomComponentActivator < ComponentWithInjectProperties > ( ) ) ;
6247
6348 // Act
6449 var instance = factory . InstantiateComponent ( GetServiceProvider ( ) , componentType ) ;
6550
6651 // Assert
6752 Assert . NotNull ( instance ) ;
68- var component = Assert . IsType < ComponentWithInjectProperties > ( instance ) ;
53+ var component = Assert . IsType < ComponentWithInjectProperties > ( instance ) ; // Custom activator returns a different type
54+
6955 // Public, and non-public properties, and properties with non-public setters should get assigned
7056 Assert . NotNull ( component . Property1 ) ;
7157 Assert . NotNull ( component . GetProperty2 ( ) ) ;
7258 Assert . NotNull ( component . Property3 ) ;
7359 Assert . NotNull ( component . Property4 ) ;
7460 }
7561
62+ [ Fact ]
63+ public void InstantiateComponent_ThrowsForNullInstance ( )
64+ {
65+ // Arrange
66+ var componentType = typeof ( EmptyComponent ) ;
67+ var factory = new ComponentFactory ( new NullResultComponentActivator ( ) ) ;
68+
69+ // Act
70+ var ex = Assert . Throws < InvalidOperationException > ( ( ) => factory . InstantiateComponent ( GetServiceProvider ( ) , componentType ) ) ;
71+ Assert . Equal ( $ "The component activator returned a null value for a component of type { componentType . FullName } .", ex . Message ) ;
72+ }
73+
7674 [ Fact ]
7775 public void InstantiateComponent_AssignsPropertiesWithInjectAttributeOnBaseType ( )
7876 {
7977 // Arrange
8078 var componentType = typeof ( DerivedComponent ) ;
81- var factory = new ComponentFactory ( ) ;
79+ var factory = new ComponentFactory ( new CustomComponentActivator < DerivedComponent > ( ) ) ;
8280
8381 // Act
8482 var instance = factory . InstantiateComponent ( GetServiceProvider ( ) , componentType ) ;
@@ -101,7 +99,7 @@ public void InstantiateComponent_IgnoresPropertiesWithoutInjectAttribute()
10199 {
102100 // Arrange
103101 var componentType = typeof ( ComponentWithNonInjectableProperties ) ;
104- var factory = new ComponentFactory ( ) ;
102+ var factory = new ComponentFactory ( new DefaultComponentActivator ( ) ) ;
105103
106104 // Act
107105 var instance = factory . InstantiateComponent ( GetServiceProvider ( ) , componentType ) ;
@@ -122,15 +120,6 @@ private static IServiceProvider GetServiceProvider()
122120 . BuildServiceProvider ( ) ;
123121 }
124122
125- private static IServiceProvider GetServiceProviderWithActivator ( )
126- {
127- return new ServiceCollection ( )
128- . AddTransient < TestService1 > ( )
129- . AddTransient < TestService2 > ( )
130- . AddSingleton < IComponentActivator , DefaultComponentActivator > ( )
131- . BuildServiceProvider ( ) ;
132- }
133-
134123 private class EmptyComponent : IComponent
135124 {
136125 public void Attach ( RenderHandle renderHandle )
@@ -197,9 +186,23 @@ private class DerivedComponent : ComponentWithInjectProperties
197186 public TestService2 Property5 { get ; set ; }
198187 }
199188
200- private class NonComponent { }
201-
202189 public class TestService1 { }
203190 public class TestService2 { }
191+
192+ private class CustomComponentActivator < TResult > : IComponentActivator where TResult : IComponent , new ( )
193+ {
194+ public IComponent CreateInstance ( Type componentType )
195+ {
196+ return new TResult ( ) ;
197+ }
198+ }
199+
200+ private class NullResultComponentActivator : IComponentActivator
201+ {
202+ public IComponent CreateInstance ( Type componentType )
203+ {
204+ return null ;
205+ }
206+ }
204207 }
205208}
0 commit comments