Skip to content

Commit a332c10

Browse files
committed
#262 Drafted the implementation of support user secrets
1 parent fd650ad commit a332c10

File tree

5 files changed

+60
-28
lines changed

5 files changed

+60
-28
lines changed

examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Fixtures/TestProjectFixture.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ protected override void AddServices(IServiceCollection services, IConfiguration?
77
.AddTransient<ICalculator, Calculator>()
88
.AddKeyedTransient<ICarMaker, Porsche>("Porsche")
99
.AddKeyedTransient<ICarMaker, Toyota>("Toyota")
10-
.Configure<Options>(config => configuration?.GetSection("Options").Bind(config));
10+
.Configure<Options>(config => configuration?.GetSection("Options").Bind(config))
11+
.Configure<SecretValues>(config => configuration?.GetSection(nameof(SecretValues)));
1112

1213
protected override ValueTask DisposeAsyncCore()
1314
=> new();
@@ -16,4 +17,7 @@ protected override IEnumerable<TestAppSettings> GetTestAppSettings()
1617
{
1718
yield return new() { Filename = "appsettings.json", IsOptional = false };
1819
}
20+
21+
protected override void AddUserSecrets(IConfigurationBuilder configurationBuilder)
22+
=> configurationBuilder.AddUserSecrets<TestProjectFixture>();
1923
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Xunit.Microsoft.DependencyInjection.ExampleTests;
2+
3+
public record SecretValues
4+
{
5+
public string? Secret1 { get; set; }
6+
public string? Secret2 { get; set; }
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+

2+
using Microsoft.Extensions.Options;
3+
4+
namespace Xunit.Microsoft.DependencyInjection.ExampleTests;
5+
6+
public class UserSecretTests(ITestOutputHelper testOutputHelper, TestProjectFixture fixture) : TestBed<TestProjectFixture>(testOutputHelper, fixture)
7+
{
8+
[Fact]
9+
public void TestSecretValues()
10+
{
11+
var secretValues = _fixture.GetService<IOptions<SecretValues>>(_testOutputHelper)!.Value;
12+
Assert.NotEmpty(secretValues?.Secret1 ?? string.Empty);
13+
Assert.NotEmpty(secretValues?.Secret1 ?? string.Empty);
14+
}
15+
}

examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Xunit.Microsoft.DependencyInjection.ExampleTests.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
<IsPackable>false</IsPackable>
77
<Nullable>enable</Nullable>
88
<ImplicitUsings>enable</ImplicitUsings>
9+
<UserSecretsId>59bdc82c-5628-47c8-a5ec-3630c3a2bc45</UserSecretsId>
910
</PropertyGroup>
1011

1112
<ItemGroup>
13+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
1214
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
13-
<PackageReference Include="xunit" Version="2.8.1" />
14-
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
15+
<PackageReference Include="xunit" Version="2.9.0" />
16+
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
1517
<PrivateAssets>all</PrivateAssets>
1618
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1719
</PackageReference>

src/Abstracts/TestBedFixture.cs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ protected TestBedFixture()
1414
{
1515
_services = new ServiceCollection();
1616
ConfigurationBuilder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory());
17+
AddUserSecrets(ConfigurationBuilder);
1718
Configuration = GetConfigurationRoot();
1819
_servicesAdded = false;
1920
}
@@ -30,9 +31,10 @@ public ServiceProvider GetServiceProvider(ITestOutputHelper testOutputHelper)
3031
if(!_servicesAdded)
3132
{
3233
AddServices(_services, Configuration);
34+
_services.AddLogging(loggingBuilder => AddLoggingProvider(loggingBuilder, new OutputLoggerProvider(testOutputHelper)));
35+
_services.AddOptions();
3336
_servicesAdded = true;
3437
}
35-
_services.AddLogging(loggingBuilder => AddLoggingProvider(loggingBuilder, new OutputLoggerProvider(testOutputHelper)));
3638
return _serviceProvider = _services.BuildServiceProvider();
3739
}
3840

@@ -55,12 +57,38 @@ public AsyncServiceScope GetAsyncScope(ITestOutputHelper testOutputHelper)
5557
public T? GetKeyedService<T>([DisallowNull] string key, ITestOutputHelper testOutputHelper)
5658
=> GetServiceProvider(testOutputHelper).GetKeyedService<T>(key);
5759

60+
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
61+
// ~AbstractDependencyInjectionFixture()
62+
// {
63+
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
64+
// Dispose(disposing: false);
65+
// }
66+
67+
public void Dispose()
68+
{
69+
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
70+
Dispose(disposing: true);
71+
GC.SuppressFinalize(this);
72+
}
73+
74+
public async ValueTask DisposeAsync()
75+
{
76+
if (!_disposedAsync)
77+
{
78+
await DisposeAsyncCore();
79+
Dispose();
80+
_disposedAsync = true;
81+
}
82+
}
83+
5884
protected abstract void AddServices(IServiceCollection services, IConfiguration? configuration);
5985
protected abstract IEnumerable<TestAppSettings> GetTestAppSettings();
6086

6187
protected virtual ILoggingBuilder AddLoggingProvider(ILoggingBuilder loggingBuilder, ILoggerProvider loggerProvider)
6288
=> loggingBuilder.AddProvider(loggerProvider);
6389

90+
protected virtual void AddUserSecrets(IConfigurationBuilder configurationBuilder) { }
91+
6492
private IConfigurationRoot? GetConfigurationRoot()
6593
{
6694
var testAppSettings = GetTestAppSettings();
@@ -100,29 +128,5 @@ protected virtual void Dispose(bool disposing)
100128
}
101129
}
102130

103-
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
104-
// ~AbstractDependencyInjectionFixture()
105-
// {
106-
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
107-
// Dispose(disposing: false);
108-
// }
109-
110-
public void Dispose()
111-
{
112-
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
113-
Dispose(disposing: true);
114-
GC.SuppressFinalize(this);
115-
}
116-
117-
public async ValueTask DisposeAsync()
118-
{
119-
if (!_disposedAsync)
120-
{
121-
await DisposeAsyncCore();
122-
Dispose();
123-
_disposedAsync = true;
124-
}
125-
}
126-
127131
protected abstract ValueTask DisposeAsyncCore();
128132
}

0 commit comments

Comments
 (0)