/AspNet.ConfigurationFaker

Easily change aspnet core configurations on your integration tests

Primary LanguageC#MIT LicenseMIT

CI Nuget

https://editorconfig.org/

AspNet.ConfigurationFaker

Simple change AspNet Core configuration on integration tests

Getting started

NuGet package available:

$ dotnet add package AspNet.ConfigurationFaker

How To Use:

class TestFixture : WebApplicationFactory<Program>
{
    FakeConfigurationProvider FakeConfig { get; } = new();

    protected override void ConfigureWebHost(IWebHostBuilder builder) => builder
        .UseFakeConfigurationProvider(FakeConfig) // for startup/program config mock
        .ConfigureAppConfiguration(config =>
        {
            config.RemoveJsonSource("appsettings.Local.json");

            config.AddFakeConfiguration(FakeConfig);

            FakeConfig.ReplaceConfigurationUrls(config, "wiremock", "http://localhost:1234");
            // replaces any "http://wiremock" value on test config for http://localhost:1234

        })
        .ConfigureTestServices(services =>
        {
            /* ... */
        });

    [OneTimeSetUp]
    public void OneTimeSetup()
    {
        FakeConfig.Add("ApiKey", Guid.NewGuid());
        FakeConfig.Freeze();
    }

    [SetUp]
    public void Setup() =>
        FakeConfig.Reset(); // resets to the last frozen state

    [Test]
    public void ConfigTest()
    {
        FakeConfig.Add("Parent:ChildValue", 42);

        FakeConfig.AddJson(
            """
            {
              "Parent": {
                "ChildValue": 42
              }
            }
            """);

        FakeConfig.AddAsJson(new
        {
            Parent = new
            {
                ChildValue = 42,
            },
        });

        /* Code that injects IConfiguration or IOptions */
    }
}
);