Skip to content

Commit 9243f82

Browse files
committed
Refactor DI test constructors and update namespaces #313
Simplifies test class constructors by using C# primary constructor syntax and removes redundant constructor definitions. Updates CalculatorService namespace and type references for consistency. Refactors TestBedFactoryFixture to use collection expressions and improves method invocation logic.
1 parent ea51681 commit 9243f82

File tree

5 files changed

+11
-25
lines changed

5 files changed

+11
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ T? GetKeyedService<T>([DisallowNull] string key, ITestOutputHelper testOutputHel
7979

8080
## Constructor Dependency Injection
8181

82-
**New in this version**: The library now supports constructor-style dependency injection while maintaining full backward compatibility with the existing fixture-based approach.
82+
**New in this version (ver 9.2.0 and beyond)**: The library now supports constructor-style dependency injection while maintaining full backward compatibility with the existing fixture-based approach.
8383

8484
### Property Injection with TestBedWithDI (Recommended)
8585

examples/Xunit.Microsoft.DependencyInjection.ExampleTests/AdvancedDependencyInjectionTests.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Xunit.Microsoft.DependencyInjection.ExampleTests;
77
/// <summary>
88
/// Example tests demonstrating advanced dependency injection patterns including IOptions, Func&lt;T&gt;, and Action&lt;T&gt;
99
/// </summary>
10-
public class AdvancedDependencyInjectionTests : TestBedWithDI<TestProjectFixture>
10+
public class AdvancedDependencyInjectionTests(ITestOutputHelper testOutputHelper, TestProjectFixture fixture) : TestBedWithDI<TestProjectFixture>(testOutputHelper, fixture)
1111
{
1212
[Inject]
1313
private IOptions<Options>? Options { get; set; }
@@ -27,11 +27,6 @@ public class AdvancedDependencyInjectionTests : TestBedWithDI<TestProjectFixture
2727
[Inject]
2828
private ISingletonService? SingletonService { get; set; }
2929

30-
public AdvancedDependencyInjectionTests(ITestOutputHelper testOutputHelper, TestProjectFixture fixture)
31-
: base(testOutputHelper, fixture)
32-
{
33-
}
34-
3530
[Fact]
3631
public void TestIOptionsPatterns()
3732
{

examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Services/CalculatorService.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using Microsoft.Extensions.DependencyInjection;
21
using Microsoft.Extensions.Options;
3-
using Xunit.Microsoft.DependencyInjection.Attributes;
42

5-
namespace Xunit.Microsoft.DependencyInjection.ExampleTests;
3+
namespace Xunit.Microsoft.DependencyInjection.ExampleTests.Services;
64

75
/// <summary>
86
/// Example class that demonstrates constructor injection pattern
@@ -12,13 +10,13 @@ namespace Xunit.Microsoft.DependencyInjection.ExampleTests;
1210
public class CalculatorService
1311
{
1412
private readonly ICalculator _calculator;
15-
private readonly Services.Options _options;
13+
private readonly Options _options;
1614
private readonly ICarMaker _porsche;
1715
private readonly ICarMaker _toyota;
1816

1917
internal CalculatorService(
2018
ICalculator calculator,
21-
IOptions<Services.Options> options,
19+
IOptions<Options> options,
2220
[FromKeyedService("Porsche")] ICarMaker porsche,
2321
[FromKeyedService("Toyota")] ICarMaker toyota)
2422
{

examples/Xunit.Microsoft.DependencyInjection.ExampleTests/TransientServiceTests.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Xunit.Microsoft.DependencyInjection.ExampleTests;
88
/// Example tests demonstrating transient service injection using property injection pattern
99
/// Transient services create a new instance for each injection point
1010
/// </summary>
11-
public class TransientServiceTests : TestBedWithDI<TestProjectFixture>
11+
public class TransientServiceTests(ITestOutputHelper testOutputHelper, TestProjectFixture fixture) : TestBedWithDI<TestProjectFixture>(testOutputHelper, fixture)
1212
{
1313
[Inject]
1414
private ITransientService? TransientService1 { get; set; }
@@ -22,11 +22,6 @@ public class TransientServiceTests : TestBedWithDI<TestProjectFixture>
2222
[Inject]
2323
private IOptions<Options>? Options { get; set; }
2424

25-
public TransientServiceTests(ITestOutputHelper testOutputHelper, TestProjectFixture fixture)
26-
: base(testOutputHelper, fixture)
27-
{
28-
}
29-
3025
[Fact]
3126
public async Task TestTransientServiceCreatesDifferentInstances()
3227
{
@@ -143,7 +138,7 @@ public void TestTransientServiceWithActionDelegate()
143138
// Assert - Should have worked with 3 different instances
144139
Assert.Equal(3, results.Count);
145140
Assert.Equal(3, instanceIds.Count);
146-
Assert.True(instanceIds.Distinct().Count() == 3); // All different
141+
Assert.Equal(3, instanceIds.Distinct().Count()); // All different
147142

148143
// Verify results contain expected patterns
149144
Assert.All(results, result =>

src/Abstracts/TestBedFactoryFixture.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using Microsoft.Extensions.DependencyInjection;
21
using System.Reflection;
32

43
namespace Xunit.Microsoft.DependencyInjection.Abstracts;
@@ -51,7 +50,7 @@ private object CreateInstance(Type testType, IServiceProvider serviceProvider, I
5150
{
5251
var parameters = constructor.GetParameters();
5352
var args = new List<object?>();
54-
bool canResolveAll = true;
53+
var canResolveAll = true;
5554

5655
foreach (var parameter in parameters)
5756
{
@@ -102,9 +101,8 @@ private object CreateInstance(Type testType, IServiceProvider serviceProvider, I
102101
// Use the fixture's existing GetKeyedService method instead of reflection
103102
try
104103
{
105-
var getKeyedServiceMethod = GetType().BaseType?.GetMethod("GetKeyedService",
106-
new[] { typeof(string), typeof(ITestOutputHelper) })?.MakeGenericMethod(parameter.ParameterType);
107-
arg = getKeyedServiceMethod?.Invoke(this, new object[] { key, testOutputHelper });
104+
var getKeyedServiceMethod = GetType().BaseType?.GetMethod("GetKeyedService",[typeof(string), typeof(ITestOutputHelper)])?.MakeGenericMethod(parameter.ParameterType);
105+
arg = getKeyedServiceMethod?.Invoke(this, [key, testOutputHelper]);
108106
}
109107
catch
110108
{
@@ -141,7 +139,7 @@ private object CreateInstance(Type testType, IServiceProvider serviceProvider, I
141139
try
142140
{
143141
// Use constructor.Invoke for better control over internal constructors
144-
return constructor.Invoke(args.ToArray())!;
142+
return constructor.Invoke([.. args])!;
145143
}
146144
catch
147145
{

0 commit comments

Comments
 (0)