Skip to content

Commit ca05132

Browse files
authored
Merge pull request #334 from ChrisDoernen/defer-building-configuration
Defer adding user secrets and building configuration
2 parents 64e3354 + 983c0dc commit ca05132

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace Xunit.Microsoft.DependencyInjection.ExampleTests;
2+
3+
public class ConfigurationTests : TestBed<TestProjectFixture>
4+
{
5+
private const string Key = "CONFIG_KEY";
6+
private const string Value = "Value";
7+
8+
public ConfigurationTests(ITestOutputHelper testOutputHelper, TestProjectFixture fixture) : base(testOutputHelper, fixture)
9+
{
10+
Environment.SetEnvironmentVariable(Key, Value);
11+
}
12+
13+
[Fact]
14+
public void EnvironmentVariablesViaConstructorAreAvailable()
15+
{
16+
_fixture.GetServiceProvider(_testOutputHelper);
17+
18+
var value = _fixture.Configuration!.GetValue<string>(Key);
19+
20+
Assert.Equal(Value, value);
21+
}
22+
}

src/Abstracts/TestBedFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ protected TestBedFixture()
2323
{
2424
_services = new ServiceCollection();
2525
ConfigurationBuilder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory());
26-
AddUserSecrets(ConfigurationBuilder);
27-
Configuration = GetConfigurationRoot();
2826
_servicesAdded = false;
2927
}
3028

@@ -51,6 +49,8 @@ public ServiceProvider GetServiceProvider(ITestOutputHelper testOutputHelper)
5149
}
5250
if (!_servicesAdded)
5351
{
52+
AddUserSecrets(ConfigurationBuilder);
53+
Configuration = GetConfigurationRoot();
5454
AddServices(_services, Configuration);
5555
_services.AddLogging(loggingBuilder => AddLoggingProvider(loggingBuilder, new OutputLoggerProvider(testOutputHelper)));
5656
_services.AddOptions();

0 commit comments

Comments
 (0)