Skip to content

Commit 3beb4da

Browse files
authored
Defer calling AddServices
When placed in the constructor, AddServices is called before the constructor of the deriving fixtures runs. This is suboptimal in cases where the implementation of AddServices depends on something that happens in that constructor. Examples is loading env variables (dotenv.net package) or bootstrapping a WebApplicationFactory like in ASP.NET integration tests.
1 parent 1727b22 commit 3beb4da

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/Abstracts/TestBedFixture.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ protected TestBedFixture()
1212
_services = new ServiceCollection();
1313
ConfigurationBuilder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory());
1414
Configuration = GetConfigurationRoot();
15-
AddServices(_services, Configuration);
1615
}
1716

1817
public IConfigurationRoot? Configuration { get; private set; }
@@ -25,6 +24,8 @@ public IServiceProvider GetServiceProvider(ITestOutputHelper testOutputHelper)
2524
return _serviceProvider;
2625
}
2726

27+
AddServices(_services, Configuration);
28+
2829
_services.AddLogging(loggingBuilder => AddLoggingProvider(loggingBuilder, new OutputLoggerProvider(testOutputHelper)));
2930
return _serviceProvider = _services.BuildServiceProvider();
3031
}

0 commit comments

Comments
 (0)