Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test workaround #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
name: Run tests
on:
push:
branches:
- main
on: [push, pull_request]

jobs:
runtests:
Expand Down
31 changes: 31 additions & 0 deletions src/Demo.Api/MinimalApiTestConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Microsoft.Extensions.Configuration;

public static class MinimalApiTestConfiguration
{
// This async local is set in from tests and it flows to main
internal static readonly AsyncLocal<Action<IConfigurationBuilder>?> _current = new();

/// <summary>
/// Adds the current test configuration to the application in the "right" place
/// </summary>
/// <param name="configurationBuilder">The configuration builder</param>
/// <returns>The modified <see cref="IConfigurationBuilder"/></returns>
public static IConfigurationBuilder AddTestConfiguration(this IConfigurationBuilder configurationBuilder)
{
if (_current.Value is { } configure)
{
configure(configurationBuilder);
}

return configurationBuilder;
}

/// <summary>
/// Unit tests can use this to flow state to the main program and change configuration
/// </summary>
/// <param name="action"></param>
public static void Configure(Action<IConfigurationBuilder> action)
{
_current.Value = action;
}
}
2 changes: 2 additions & 0 deletions src/Demo.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddTestConfiguration();

builder.Services.AddSingleton(new Options(builder.Configuration["AppSettings:Options"]));

var app = builder.Build();
Expand Down
23 changes: 11 additions & 12 deletions test/Demo.Test/TestServerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@ public class TestServerFixture : WebApplicationFactory<Program>
{
internal Dictionary<string, string> OverrideConfiguration = new();

protected override void ConfigureWebHost(IWebHostBuilder builder)
protected override IWebHostBuilder? CreateWebHostBuilder()
{
base.ConfigureWebHost(builder);
MinimalApiTestConfiguration.Configure(builder =>
{
System.Console.WriteLine("configure test");
var testDir = Path.GetDirectoryName(GetType().Assembly.Location);
var configLocation = Path.Combine(testDir!, "testsettings.json");

builder
.ConfigureAppConfiguration((ctx, builder) =>
{
System.Console.WriteLine("configure test");
var testDir = Path.GetDirectoryName(GetType().Assembly.Location);
var configLocation = Path.Combine(testDir!, "testsettings.json");
builder.Sources.Clear();
builder.AddJsonFile(configLocation);
builder.AddInMemoryCollection(OverrideConfiguration);
});

builder.Sources.Clear();
builder.AddJsonFile(configLocation);
builder.AddInMemoryCollection(OverrideConfiguration);
});
return base.CreateWebHostBuilder();
}
}