Mock connection to Azure App Configuration service using Moq

1.1k Views Asked by At

I would like to mock connection to Azure App Configuration (feature flag) service using DefaultAzureCredential and Moq framework.

I wrote extension based on Microsoft tutorial https://learn.microsoft.com/en-us/azure/azure-app-configuration/quickstart-feature-flag-aspnet-core?tabs=core6x%2Ccore5x

I'm using it in Program.cs

public static WebApplicationBuilder UseFeatureFlags(this WebApplicationBuilder hostBuilder)
    {
        var endpoint = hostBuilder.Configuration.GetValue<string>("Azure:AppConfig:Endpoint");
        var cacheExpirationInterval = hostBuilder.Configuration.GetValue<int>("FeatureManagement:CacheExpirationInterval");
        var label = hostBuilder.Configuration.GetValue<string>("FeatureManagement:Label");

        hostBuilder.Host
            .ConfigureAppConfiguration((builder, config) =>
                config.AddAzureAppConfiguration(options =>
                      options.Connect(new Uri(endpoint), new DefaultAzureCredential())
                             .UseFeatureFlags(featureFlagOptions =>
                             {
                                 featureFlagOptions.CacheExpirationInterval = TimeSpan.FromMinutes(cacheExpirationInterval);
                                 featureFlagOptions.Label = label;
                             })));

        return hostBuilder;
    }

Now I'm trying to fix my unit tests since they are failing in my WebApplicationFactory (401 unauthorized) on line

options.Connect(new Uri(endpoint), new DefaultAzureCredential())

Is there a simple way to mock it? Here is part of my Api WebApplicationFactory

public class ApiWebApplicationFactory : WebApplicationFactory<Program>
{
    public HttpClient WithMocks(
        IMock<ISecretVault>? secretVaultMock = null,
        IMock<IFeatureManager>? featureManager = null)
    {
        var client = WithWebHostBuilder(builder =>
            builder.ConfigureServices(services =>
            {
                ReplaceWithMock(typeof(ISecretVault), secretVaultMock, services);
                ReplaceWithMock(typeof(IFeatureManager), featureManager, services);
            })).CreateClient();


        return client;
    }

    private static void ReplaceWithMock<T>(Type tgt, IMock<T>? mock, IServiceCollection services)
        where T : class
    {
        if (mock != null)
        {
            var serviceClientDescriptor = services.Single(d => d.ServiceType == tgt);
            services.Remove(serviceClientDescriptor);
            services.AddScoped(_ => mock.Object);
        }
    }
}

Thank you in advance for any tip or sample code

1

There are 1 best solutions below

0
On

Typically when Mocking you are within Development or Unit Testing.

So the easiest option we have found it to simple not use Azure App config in development or tests, as this breaks the rules of no external dependences. AAC is just a wrapper to inject values into the ConfigurationBuilder. So really, it's got no purpose within Unit Testing and development.

Only in UAT, Staging and Production should the setting be read from AAC IMO.

Don't mock just don't use