@@ -34,7 +34,7 @@ Always reference these instructions first and fallback to search or bash command
34
34
cd ../examples/Xunit.Microsoft.DependencyInjection.ExampleTests
35
35
dotnet test --configuration Release
36
36
```
37
- -- takes ~ 10.8 seconds with 9 tests passing. NEVER CANCEL. Set timeout to 60+ seconds.
37
+ -- takes ~ 10.8 seconds with 43 tests passing. NEVER CANCEL. Set timeout to 60+ seconds.
38
38
- ** Package library** : ` dotnet pack --configuration Release ` -- takes ~ 1.9 seconds. NEVER CANCEL. Set timeout to 30+ seconds.
39
39
40
40
### Code Quality and Formatting
@@ -49,11 +49,59 @@ Always reference these instructions first and fallback to search or bash command
49
49
- The library targets ` net9.0 ` framework exclusively
50
50
- Use Visual Studio Code tasks defined in ` .vscode/tasks.json ` for build, publish, and watch operations
51
51
52
+ ## Understanding Test Patterns
53
+
54
+ The library supports multiple dependency injection approaches:
55
+
56
+ ### 1. Traditional Fixture-Based (Fully Supported)
57
+ ``` csharp
58
+ public class MyTests : TestBed <TestProjectFixture >
59
+ {
60
+ [Fact ]
61
+ public async Task TestCalculation ()
62
+ {
63
+ var calculator = _fixture .GetService <ICalculator >(_testOutputHelper );
64
+ var result = await calculator .AddAsync (1 , 2 );
65
+ Assert .Equal (3 , result );
66
+ }
67
+ }
68
+ ```
69
+
70
+ ### 2. Property Injection (Recommended - New in 9.2.0+)
71
+ ``` csharp
72
+ public class MyTests : TestBedWithDI <TestProjectFixture >
73
+ {
74
+ [Inject ] private ICalculator Calculator { get ; set ; } = null ! ;
75
+ [Inject (" Porsche" )] private ICarMaker PorscheMaker { get ; set ; } = null ! ;
76
+
77
+ [Fact ]
78
+ public async Task TestCalculation ()
79
+ {
80
+ var result = await Calculator .AddAsync (1 , 2 );
81
+ Assert .Equal (3 , result );
82
+ }
83
+ }
84
+ ```
85
+
86
+ ### 3. Factory Pattern (Experimental)
87
+ ``` csharp
88
+ public class MyTests : TestBed <FactoryTestProjectFixture >
89
+ {
90
+ [Fact ]
91
+ public async Task TestConstructorInjection ()
92
+ {
93
+ var service = _fixture .CreateTestInstance <SimpleService >(_testOutputHelper );
94
+ var result = await service .CalculateAsync (10 , 5 );
95
+ Assert .True (result > 0 );
96
+ }
97
+ }
98
+ ```
99
+
52
100
## Validation Scenarios
53
101
54
102
After making any changes to the library code:
55
103
1 . ** Build validation** : Run ` dotnet build --configuration Release ` and ensure it completes successfully
56
- 2 . ** Test validation** : Run example tests with ` dotnet test --configuration Release ` and verify all 9 tests pass
104
+ 2 . ** Test validation** : Run example tests with ` dotnet test --configuration Release ` and verify all 43 tests pass
57
105
3 . ** Format validation** : Run ` dotnet format ` to ensure code follows project standards
58
106
4 . ** Package validation** : Run ` dotnet pack --configuration Release ` to ensure the library can be packaged
59
107
@@ -65,6 +113,25 @@ The example tests demonstrate complete usage patterns:
65
113
- ** Configuration binding** : Tests demonstrate configuration file and user secrets integration
66
114
- ** Test ordering** : Tests show the test ordering feature with ` TestOrder ` attributes
67
115
116
+ #### Testing Specific Features
117
+ Run individual test scenarios to validate changes:
118
+ ``` bash
119
+ # Test property injection (new dependency injection pattern)
120
+ dotnet test --filter " TestCalculatorThroughPropertyInjection" --configuration Release
121
+
122
+ # Test keyed services (Porsche/Toyota car makers)
123
+ dotnet test --filter " GetKeyedService" --configuration Release
124
+
125
+ # Test factory pattern (constructor injection)
126
+ dotnet test --filter " TestConstructorInjectionViaFactory" --configuration Release
127
+
128
+ # Test configuration and user secrets
129
+ dotnet test --filter " TestSecretValues" --configuration Release
130
+
131
+ # List all available tests
132
+ dotnet test --list-tests --configuration Release
133
+ ```
134
+
68
135
## Project Structure
69
136
70
137
### Key Directories and Files
@@ -99,24 +166,34 @@ The example tests demonstrate complete usage patterns:
99
166
- ** Format Issues** : Run ` dotnet format ` to auto-fix most style problems
100
167
- ** Missing Dependencies** : Run ` dotnet restore ` to restore NuGet packages
101
168
169
+ #### Common Validation Failures
170
+ - ** Build succeeds but tests fail** : Check if you're in the correct directory (` examples/Xunit.Microsoft.DependencyInjection.ExampleTests ` )
171
+ - ** "No tests found" error** : Verify test filter syntax with ` dotnet test --list-tests ` first
172
+ - ** Timeout during restore/build** : DO NOT CANCEL - operations take 8-15 seconds normally, set 60+ second timeouts
173
+ - ** SourceLink warnings** : These are normal during format validation and can be ignored
174
+ - ** Test count mismatch** : Current test suite has 43 tests; if you see different counts, investigate test changes
175
+
102
176
### Working with Examples
103
177
The examples project is a fully functional test suite that demonstrates:
104
- - Service registration and dependency injection patterns
105
- - Configuration file usage with ` appsettings.json `
106
- - User secrets integration for sensitive data
107
- - Keyed services (Porsche/Toyota car maker examples)
108
- - Test ordering with custom attributes
109
- - Both synchronous and asynchronous test patterns
178
+ - ** Traditional fixture-based approach** : See ` CalculatorTests.cs ` using ` TestBed<TFixture> ` and ` _fixture.GetService<T>(_testOutputHelper) `
179
+ - ** Property injection approach** : See ` PropertyInjectionTests.cs ` using ` TestBedWithDI<TFixture> ` with ` [Inject] ` attributes
180
+ - ** Factory constructor injection** : See ` FactoryConstructorInjectionTests.cs ` for experimental true constructor injection
181
+ - ** Service registration patterns** : Multiple service lifetimes (transient, scoped, singleton)
182
+ - ** Configuration file usage** : ` appsettings.json ` integration
183
+ - ** User secrets integration** : Sensitive data handling for development
184
+ - ** Keyed services** : Porsche/Toyota car maker examples demonstrating .NET 9.0 keyed services
185
+ - ** Test ordering** : Custom attributes for controlling test execution order
186
+ - ** Advanced patterns** : ` Func<T> ` , ` Action<T> ` , and ` IOptions<T> ` injection patterns
110
187
111
188
Always use the examples to validate that your changes don't break real-world usage scenarios.
112
189
113
190
## Build Times and Performance Expectations
114
191
- ** Package restore** : ~ 1-8 seconds (varies with cache state)
115
192
- ** Build (Release)** : ~ 4-6 seconds
116
- - ** Test execution** : ~ 9-11 seconds (9 tests pass)
193
+ - ** Test execution** : ~ 9-11 seconds (43 tests pass)
117
194
- ** Code formatting** : ~ 7-10 seconds
118
195
- ** Package creation** : ~ 1-2 seconds
119
- - ** Complete workflow** : ~ 20-25 seconds total
196
+ - ** Complete workflow** : ~ 25-35 seconds total
120
197
121
198
** CRITICAL** : NEVER CANCEL builds or tests. These times are normal. Set timeouts to 2-5 minutes to catch actual hanging processes, but actual operations complete much faster.
122
199
0 commit comments