Azure / Azure/AppConfiguration-DotnetProvider

Support for Mocking and Integration Testing

Open
#668 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
93
Forks
46
Avg merge
3d 1h
Merged PRs (30d)
3

Description

Hi, I'm trying to use Azure App Configuration for an ASP.NET minimal API web application, and was curious about how to properly implement mocking for app configuration.

It seems that `builder.Configuration.AddAzureAppConfiguration` [eagerly connects](https://github.com/Azure/AppConfiguration-DotnetProvider/blob/8b883cce448004c901bb9ef3a855ee6576bd2281/src/Microsoft.Extensions.Configuration.AzureAppConfiguration/AzureAppConfigurationProvider.cs#L844) to the specified configuration store instead of deferring connection until the Configuration is needed. This is the problem in integration testing, where the [canonical method](https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-9.0&pivots=xunit#basic-tests-with-the-default-webapplicationfactory) of creating a test server for integration tests still requires a valid AppConfiguration endpoint to connect to even if the service collection gets overriden in the SUT.

Are there any workarounds to this problem? I need to be able to run integration tests without connecting to a live appconfiguration instance, and I'd like to do so without cluttering my builder pipeline with branching if statements.

Providing a slimmed down version of my Program.cs and integration test server setup below.

```
// Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddAzureAppConfiguration(options =>
{
options.Connect(new Uri("https://myconfigstore.azconfig.io"), new DefaultAzureCredential())
.ConfigureRefresh(refreshOptions =>
refreshOptions
.Register(key: "SentinelKey", refreshAll: true))
.UseFeatureFlags();
});

builder.Services.AddAzureAppConfiguration();
builder.Services.AddFeatureManagement();

var app = builder.Build();

app.UseAzureAppConfiguration();
app.Run();

public partial class Program { }

// IntegrationTest.cs

public class CustomWebAppFactory : WebApplicationFactory where TProgram : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
// Attempt to override the configuration
builder.ConfigureAppConfiguration(config =>
{
config.AddInMemoryCollection(new Dictionary
{
{ "hello", "world" },
});
});
}
}

public class Tests : IClassFixture>
{
private readonly CustomWebAppFactory _factory;
public RandomTest(CustomWebAppFactory factory)
{
_factory = factory;
}

[Fact]
public async Task Test1()
{
var client = _factory.CreateClient();
var response = await client.GetAsync("/hello");
response.EnsureSuccessStatusCode();
}
}
```

I'd like that the test fixture never attempts to connect to AppCOnfiguration, but empirically I do see `System.TimeoutException` as the endpoint is not set.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.