如何在测试Azure函数时调用ConfigureAppConfiguration

4si2a6ki  于 2023-05-18  发布在  其他
关注(0)|答案(2)|浏览(98)

我们最近将Azure应用程序配置添加到Azure函数中,现在我们希望通过测试触发应用程序配置的启动代码。
我们的函数启动代码看起来像这样-

public override void Configure(IFunctionsHostBuilder builder)
    {
        config = Utils.LoadConfig();
        StartupHelper.AddCommonIoCDependencies(builder);
    }

    public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
    {
        //How do we trigger this method when testing???
        StartupHelper.AddAzureAppConfiguration(builder);
    }

在运行测试时,使用以下命令触发初始配置方法-

private MyFunction_sut;

    [SetUp]
    public void Setup()
    {
        var funcStartup = new Startup();

        var host = new HostBuilder()
            .ConfigureWebJobs(funcStartup.Configure)
            .Build();

        _sut = new MyFunction(...);
    }

这适用于触发Congfigure方法。尽管HostBuilder有ConfigureAppConfiguration的选项,但我不知道如何让它工作。我觉得应该有一个快速和简单的方法与我下面的内容。

var host = new HostBuilder()
            .ConfigureWebJobs(funcStartup.Configure)
            .ConfigureAppConfiguration(x => {
                //What do I put here to call trigger the ConfigureAppConfiguration method at startup?
                //Or is it possible to call my helper "AddAzureAppConfiguration" method that is in a static class?
            })
            .Build();

如果有一种不同的方法来测试Azure函数以添加Azure应用程序配置,而不涉及HostatBuilder,那么我很乐意听到其他人的方法。我在网上搜索时真的看不到太多关于这个的东西,所以我觉得我错过了什么。
任何建议都很感激!

3gtaxfhh

3gtaxfhh1#

我需要做类似的事情。不幸的是,IFunctionsConfigurationBuilder的实现,这是完成此操作所需要的,被标记为internal,所以我不得不自己复制实现。幸运的是,它很简单。

// https://github.com/Azure/azure-functions-dotnet-extensions/blob/main/src/Extensions/DependencyInjection/FunctionsConfigurationBuilder.cs
internal class FunctionsConfigurationBuilder : IFunctionsConfigurationBuilder
{
    public FunctionsConfigurationBuilder(IConfigurationBuilder configurationBuilder, WebJobsBuilderContext webJobsBuilderContext)
    {
        ConfigurationBuilder = configurationBuilder ?? throw new ArgumentNullException(nameof(configurationBuilder));
        Context = new DefaultFunctionsHostBuilderContext(webJobsBuilderContext);
    }

    public IConfigurationBuilder ConfigurationBuilder { get; }

    public FunctionsHostBuilderContext Context { get; }
}

// https://github.com/Azure/azure-functions-dotnet-extensions/blob/main/src/Extensions/DependencyInjection/DefaultFunctionsHostBuilderContext.cs
internal class DefaultFunctionsHostBuilderContext : FunctionsHostBuilderContext
{
    public DefaultFunctionsHostBuilderContext(WebJobsBuilderContext webJobsBuilderContext)
        : base(webJobsBuilderContext)
    {
    }
}

这允许我这样设置我的测试:

public TestHost()
{
    var webJobsBuilderContext = new WebJobsBuilderContext();
    var startup = new TestStartup();
    var host = new HostBuilder()
        .ConfigureAppConfiguration((context, builder) =>
            startup.ConfigureAppConfiguration(new FunctionsConfigurationBuilder(builder, webJobsBuilderContext))
        )
        .ConfigureWebJobs((context, builder) =>
        {
            webJobsBuilderContext.Configuration = context.Configuration;
            startup.Configure(webJobsBuilderContext, builder);
        })
        .Build();

    ServiceProvider = host.Services;
}

public IServiceProvider ServiceProvider { get; }

我测试的Startup看起来像:

private class TestStartup : Startup
{
    public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
    {
        // Do anything else you need to here
        base.ConfigureAppConfiguration(builder);
    }

    public void Configure(WebJobsBuilderContext context, IWebJobsBuilder builder)
    {
        // Do anything else you need to here
        base.Configure(context, builder);
    }
}

差不多就是这样如果需要进一步的细节,请告诉我。不幸的是,这并不简单。

7lrncoxx

7lrncoxx2#

有一个ConfigureWebJobs扩展方法重载,你可以使用它:

public static IHostBuilder ConfigureWebJobs(this IHostBuilder builder,
    Action<HostBuilderContext,
    IWebJobsBuilder> configure,
    Action<JobHostOptions> configureOptions,
    Action<HostBuilderContext, IWebJobsConfigurationBuilder> configureAppConfiguration)

你可以通过调用configureAppConfiguration参数来触发ConfigureAppConfiguration,如下所示:

var webJobsBuilderContext = new WebJobsBuilderContext();

var host = new HostBuilder()
    .ConfigureWebJobs((context, builder) =>
    {
        builder.UseWebJobsStartup(typeof(TestStartup), webJobsBuilderContext, NullLoggerFactory.Instance);
    }, _ => { }, (context, builder) =>
    {
        builder.UseWebJobsConfigurationStartup<TestStartup>(webJobsBuilderContext);
    })
    .Build();

相关问题