From 3beb4da233caff1a36d1f0ac6e0051b42570312a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20D=C3=B6rnen?= <32569989+ChrisDoernen@users.noreply.github.com> Date: Wed, 20 Dec 2023 16:12:15 +0100 Subject: [PATCH 1/2] 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. --- src/Abstracts/TestBedFixture.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Abstracts/TestBedFixture.cs b/src/Abstracts/TestBedFixture.cs index 4677c86..9034341 100644 --- a/src/Abstracts/TestBedFixture.cs +++ b/src/Abstracts/TestBedFixture.cs @@ -12,7 +12,6 @@ protected TestBedFixture() _services = new ServiceCollection(); ConfigurationBuilder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()); Configuration = GetConfigurationRoot(); - AddServices(_services, Configuration); } public IConfigurationRoot? Configuration { get; private set; } @@ -25,6 +24,8 @@ public IServiceProvider GetServiceProvider(ITestOutputHelper testOutputHelper) return _serviceProvider; } + AddServices(_services, Configuration); + _services.AddLogging(loggingBuilder => AddLoggingProvider(loggingBuilder, new OutputLoggerProvider(testOutputHelper))); return _serviceProvider = _services.BuildServiceProvider(); } From b607402dda419008ae2f505fb0b017297eb02a72 Mon Sep 17 00:00:00 2001 From: "Arash A. Sabet" <26050123+Arash-Sabet@users.noreply.github.com> Date: Wed, 20 Dec 2023 20:35:23 -0500 Subject: [PATCH 2/2] Preventing the initialization of the service collection --- src/Abstracts/TestBedFixture.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Abstracts/TestBedFixture.cs b/src/Abstracts/TestBedFixture.cs index 9034341..44102d9 100644 --- a/src/Abstracts/TestBedFixture.cs +++ b/src/Abstracts/TestBedFixture.cs @@ -6,12 +6,14 @@ public abstract class TestBedFixture : IDisposable, IAsyncDisposable private IServiceProvider? _serviceProvider; private bool _disposedValue; private bool _disposedAsync; + private bool _servicesAdded; protected TestBedFixture() { _services = new ServiceCollection(); ConfigurationBuilder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()); Configuration = GetConfigurationRoot(); + _servicesAdded = false; } public IConfigurationRoot? Configuration { get; private set; } @@ -23,9 +25,11 @@ public IServiceProvider GetServiceProvider(ITestOutputHelper testOutputHelper) { return _serviceProvider; } - - AddServices(_services, Configuration); - + if(!_servicesAdded) + { + AddServices(_services, Configuration); + _servicesAdded = true; + } _services.AddLogging(loggingBuilder => AddLoggingProvider(loggingBuilder, new OutputLoggerProvider(testOutputHelper))); return _serviceProvider = _services.BuildServiceProvider(); }