From 70e20052fe62ce8bcd8706ee75d84b473e60ef84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20D=C3=B6rnen?= Date: Mon, 15 Dec 2025 14:37:46 +0100 Subject: [PATCH 1/2] Defer adding user secrets and building configuration root unit constructors of base classes did run. --- src/Abstracts/TestBedFixture.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Abstracts/TestBedFixture.cs b/src/Abstracts/TestBedFixture.cs index b1b46a2..22f64d9 100644 --- a/src/Abstracts/TestBedFixture.cs +++ b/src/Abstracts/TestBedFixture.cs @@ -23,8 +23,6 @@ protected TestBedFixture() { _services = new ServiceCollection(); ConfigurationBuilder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()); - AddUserSecrets(ConfigurationBuilder); - Configuration = GetConfigurationRoot(); _servicesAdded = false; } @@ -51,6 +49,8 @@ public ServiceProvider GetServiceProvider(ITestOutputHelper testOutputHelper) } if (!_servicesAdded) { + AddUserSecrets(ConfigurationBuilder); + Configuration = GetConfigurationRoot(); AddServices(_services, Configuration); _services.AddLogging(loggingBuilder => AddLoggingProvider(loggingBuilder, new OutputLoggerProvider(testOutputHelper))); _services.AddOptions(); From 983c0dc8a595aa6effc4625eae03917407dc189b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20D=C3=B6rnen?= Date: Mon, 15 Dec 2025 17:40:43 +0100 Subject: [PATCH 2/2] Add test --- .../ConfigurationTests.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 examples/Xunit.Microsoft.DependencyInjection.ExampleTests/ConfigurationTests.cs diff --git a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/ConfigurationTests.cs b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/ConfigurationTests.cs new file mode 100644 index 0000000..4a6de58 --- /dev/null +++ b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/ConfigurationTests.cs @@ -0,0 +1,22 @@ +namespace Xunit.Microsoft.DependencyInjection.ExampleTests; + +public class ConfigurationTests : TestBed +{ + private const string Key = "CONFIG_KEY"; + private const string Value = "Value"; + + public ConfigurationTests(ITestOutputHelper testOutputHelper, TestProjectFixture fixture) : base(testOutputHelper, fixture) + { + Environment.SetEnvironmentVariable(Key, Value); + } + + [Fact] + public void EnvironmentVariablesViaConstructorAreAvailable() + { + _fixture.GetServiceProvider(_testOutputHelper); + + var value = _fixture.Configuration!.GetValue(Key); + + Assert.Equal(Value, value); + } +}