@@ -20,8 +20,53 @@ public class DependencyInjectionTestInvoker(
20
20
: XunitTestInvoker ( test , messageBus , testClass , constructorArguments , testMethod , testMethodArguments ,
21
21
beforeAfterAttributes , aggregator , cancellationTokenSource )
22
22
{
23
- private static readonly ActivitySource ActivitySource = new ( "Xunit.DependencyInjection" , typeof ( DependencyInjectionTestInvoker ) . Assembly . GetName ( ) . Version ? . ToString ( ) ) ;
24
- private static readonly MethodInfo AsTaskMethod = new Func < ObjectMethodExecutorAwaitable , Task > ( AsTask ) . Method ;
23
+ private static readonly ActivitySource ActivitySource = new ( "Xunit.DependencyInjection" ,
24
+ typeof ( DependencyInjectionTestInvoker ) . Assembly . GetName ( ) . Version ? . ToString ( ) ) ;
25
+
26
+ private static readonly MethodInfo AsTaskMethod ;
27
+ private static readonly ConcurrentDictionary < Type , PropertyInfo [ ] > HasRequiredMembers = [ ] ;
28
+
29
+ static DependencyInjectionTestInvoker ( )
30
+ {
31
+ var method = AsTask ;
32
+
33
+ AsTaskMethod = method . Method ;
34
+ }
35
+
36
+ protected override object ? CreateTestClass ( )
37
+ {
38
+ var testClassInstance = base . CreateTestClass ( ) ;
39
+ if ( testClassInstance == null ||
40
+ HasRequiredMembers . GetOrAdd ( TestClass , GetRequiredProperties ) is not { Length : > 0 } properties )
41
+ return testClassInstance ;
42
+
43
+ foreach ( var propertyInfo in properties )
44
+ {
45
+ if ( propertyInfo . CanRead )
46
+ try
47
+ {
48
+ if ( propertyInfo . GetValue ( testClassInstance , null ) != null ) continue ;
49
+ }
50
+ catch
51
+ {
52
+ continue ;
53
+ }
54
+
55
+ propertyInfo . SetValue ( testClassInstance , propertyInfo . PropertyType == typeof ( ITestOutputHelper )
56
+ ? provider . GetRequiredService < ITestOutputHelperAccessor > ( ) . Output
57
+ : provider . GetRequiredService ( propertyInfo . PropertyType ) ) ;
58
+ }
59
+
60
+ return testClassInstance ;
61
+ }
62
+
63
+ private static PropertyInfo [ ] GetRequiredProperties ( Type testClass ) =>
64
+ ! testClass . HasRequiredMemberAttribute ( ) || testClass . GetConstructors ( ) . FirstOrDefault ( static ci =>
65
+ ! ci . IsStatic && ci . IsPublic ) is not { } ci || ci . HasSetsRequiredMembersAttribute ( )
66
+ ? [ ]
67
+ : testClass . GetProperties ( )
68
+ . Where ( p => p . SetMethod is { IsPublic : true } && p . HasRequiredMemberAttribute ( ) )
69
+ . ToArray ( ) ;
25
70
26
71
/// <inheritdoc/>
27
72
protected override async Task < decimal > InvokeTestMethodAsync ( object ? testClassInstance )
0 commit comments