@@ -19,7 +19,7 @@ namespace FluentAssertions.Autofac;
19
19
public class
20
20
RegisterGenericSourceAssertions : ReferenceTypeAssertions < IComponentContext , RegisterGenericSourceAssertions >
21
21
{
22
- private readonly Type _genericComponentTypeDefinition ;
22
+ private readonly Type _type ;
23
23
24
24
/// <inheritdoc />
25
25
/// <summary>
@@ -31,67 +31,74 @@ public class
31
31
/// <summary>
32
32
/// Initializes a new instance of the <see cref="RegisterGenericSourceAssertions" /> class.
33
33
/// </summary>
34
- /// <param name="subject">The container </param>
35
- /// <param name="genericComponentTypeDefinition ">The type that should be registered on the container</param>
36
- public RegisterGenericSourceAssertions ( IComponentContext subject , Type genericComponentTypeDefinition ) :
34
+ /// <param name="subject">The component context </param>
35
+ /// <param name="type ">The type that should be registered on the container</param>
36
+ public RegisterGenericSourceAssertions ( IComponentContext subject , Type type ) :
37
37
base ( subject )
38
38
{
39
- AssertGenericType ( genericComponentTypeDefinition ) ;
40
- _genericComponentTypeDefinition = genericComponentTypeDefinition ;
39
+ AssertGenericType ( type ) ;
40
+ _type = type ;
41
41
}
42
42
43
43
/// <summary>
44
44
/// Asserts that the specified service type can be resolved from the current <see cref="IComponentContext" />.
45
45
/// </summary>
46
- /// <param name="genericServiceTypeDefinition ">The type to resolve</param>
47
- public RegistrationAssertions As ( Type genericServiceTypeDefinition )
46
+ /// <param name="type ">The type to resolve</param>
47
+ public RegistrationAssertions As ( Type type )
48
48
{
49
- AssertGenericType ( genericServiceTypeDefinition ) ;
50
- var componentServicePairText =
51
- $ "Component={ _genericComponentTypeDefinition . FullName } Service={ genericServiceTypeDefinition . FullName } ";
52
-
53
- _genericComponentTypeDefinition . GetGenericArguments ( ) . Length . Should ( )
54
- . Be ( genericServiceTypeDefinition . GetGenericArguments ( ) . Length ,
55
- $ "the generic arguments count of both generic component and generic service must be equal. { componentServicePairText } .") ;
56
-
57
- var argumentTypes = Enumerable . Repeat ( typeof ( object ) ,
58
- _genericComponentTypeDefinition . GetGenericArguments ( ) . Length ) . ToArray ( ) ;
59
- var componentType = _genericComponentTypeDefinition . MakeGenericType ( argumentTypes ) ;
60
- var serviceType = genericServiceTypeDefinition . MakeGenericType ( argumentTypes ) ;
49
+ var serviceType = GenericServiceTypeFor ( type , out var componentType ) ;
50
+ var service = new TypedService ( serviceType ) ;
51
+ var registration = RegistrationFor ( type , service , componentType ) ;
52
+ return new RegistrationAssertions ( Subject , registration ) ;
53
+ }
61
54
62
- componentType . Should ( )
63
- . Implement ( serviceType ,
64
- $ "component must implement specified service. { componentServicePairText } .") ;
55
+ /// <summary>
56
+ /// Asserts that the specified service type can be resolved from the current <see cref="IComponentContext" />.
57
+ /// </summary>
58
+ /// <param name="serviceName">The service name</param>
59
+ /// <param name="type">The type to resolve</param>
60
+ public RegistrationAssertions Named ( string serviceName , Type type )
61
+ {
62
+ var serviceType = GenericServiceTypeFor ( type , out var componentType ) ;
63
+ var service = new KeyedService ( serviceName , serviceType ) ;
64
+ var registration = RegistrationFor ( type , service , componentType ) ;
65
+ return new RegistrationAssertions ( Subject , registration ) ;
66
+ }
65
67
66
- var registration = GetRegistrationFromSources ( serviceType ) ;
68
+ private IComponentRegistration RegistrationFor ( Type type , Service service , Type componentType )
69
+ {
70
+ var registration = RegistrationsFor ( service ) . FirstOrDefault ( ) ;
67
71
registration . Should ( )
68
- . NotBeNull (
69
- $ "it must be a registration source providing registrations for service { genericServiceTypeDefinition . FullName } ") ;
70
-
71
- registration . Activator . LimitType . Should ( )
72
- . Be ( componentType ,
73
- $ "the generic component type definition in the registration must be { _genericComponentTypeDefinition . FullName } .") ;
74
-
75
- return new RegistrationAssertions ( Subject , registration ) ;
72
+ . NotBeNull ( $ "there should be a registration source providing registrations for service { type . FullName } ") ;
73
+ registration ? . Activator . LimitType . Should ( )
74
+ . Be ( componentType , $ "the generic component type definition registered should be { _type . FullName } .") ;
75
+ return registration ;
76
76
}
77
77
78
- private IComponentRegistration GetRegistrationFromSources ( Type serviceType )
78
+ private Type GenericServiceTypeFor ( Type type , out Type componentType )
79
79
{
80
- var typedService = new TypedService ( serviceType ) ;
80
+ AssertGenericType ( type ) ;
81
+ var componentServicePairText =
82
+ $ "Component={ _type . FullName } Service={ type . FullName } ";
81
83
82
- foreach ( var registrationSource in Subject . ComponentRegistry . Sources )
83
- {
84
- var registration = registrationSource
85
- . RegistrationsFor ( typedService , Accessor )
86
- . FirstOrDefault ( ) ;
84
+ _type . GetGenericArguments ( ) . Should ( ) . HaveCount ( type . GetGenericArguments ( ) . Length ,
85
+ $ "the generic arguments count of both generic component and generic service must be equal. { componentServicePairText } .") ;
87
86
88
- if ( registration != null )
89
- {
90
- return registration ;
91
- }
92
- }
87
+ var argumentTypes = Enumerable . Repeat ( typeof ( object ) ,
88
+ _type . GetGenericArguments ( ) . Length ) . ToArray ( ) ;
89
+ componentType = _type . MakeGenericType ( argumentTypes ) ;
90
+ var serviceType = type . MakeGenericType ( argumentTypes ) ;
91
+
92
+ componentType . Should ( ) . Implement ( serviceType ,
93
+ $ "component must implement specified service. { componentServicePairText } .") ;
94
+ return serviceType ;
95
+ }
93
96
94
- return null ;
97
+ private IEnumerable < IComponentRegistration > RegistrationsFor ( Service service )
98
+ {
99
+ return Subject . ComponentRegistry . Sources
100
+ . SelectMany ( sources => sources . RegistrationsFor ( service , Accessor )
101
+ . ToList ( ) ) ;
95
102
}
96
103
97
104
private IEnumerable < ServiceRegistration > Accessor ( Service service )
@@ -100,15 +107,15 @@ private IEnumerable<ServiceRegistration> Accessor(Service service)
100
107
. Select ( c => new ServiceRegistration ( ServicePipelines . DefaultServicePipeline , c ) ) ;
101
108
}
102
109
103
- private static void AssertGenericType ( Type genericTypeDefinition )
110
+ private static void AssertGenericType ( Type type )
104
111
{
105
- if ( genericTypeDefinition == null )
106
- throw new ArgumentNullException ( nameof ( genericTypeDefinition ) ) ;
112
+ if ( type == null )
113
+ throw new ArgumentNullException ( nameof ( type ) ) ;
107
114
108
- if ( ! genericTypeDefinition . IsGenericTypeDefinition )
115
+ if ( ! type . IsGenericTypeDefinition )
109
116
{
110
117
throw new ArgumentException ( "Type must be a generic type definition." ,
111
- nameof ( genericTypeDefinition ) ) ;
118
+ nameof ( type ) ) ;
112
119
}
113
120
}
114
121
}
0 commit comments