为什么在集成测试我的 Azure Functions 时 IConfiguration 为空?
Why is IConfiguration null when integration testing my Azure Functions?
当前版本的 Microsoft.Azure.Functions.Extensions 包公开了一个额外的 属性,使您可以轻松访问提供给函数的 IConfiguration
。以前这需要手动构建一个服务提供者,这显然是有问题的。
使用那个包我的 FunctionsStartup.cs
看起来像这样:
public override void Configure(IFunctionsHostBuilder builder)
{
base.Configure(builder);
var config = builder.GetContext().Configuration; // new in v1.1.0 of Microsoft.Azure.Functions.Extensions
var mySetting = config["MySetting"];
int.Parse(mySetting, out var mySetting);
// ... use mySetting...
}
为了测试我的 HTTP 触发函数,我使用 this article 作为基础,它详细说明了如何手动构建和启动主机来执行我的函数,就好像它是 运行在 Azure 中运行,类似于 TestServer
在 ASP.NET 中的工作方式 Core:
var host = new HostBuilder()
.ConfigureWebJobs(new FunctionsStartup().Configure)
.Build();
var functionsInstance = ActivatorUtilities.CreateInstance<MyFunctions>(host.Services);
然后我可以执行 MyFunctions
上定义的函数方法来测试它们的响应:
var request = new DefaultHttpRequest(new DefaultHttpContext());
var response = (OkObjectResult)functionsInstance.HttpTriggerMethod(request);
... assert that response is valid
问题是,当我 运行 我的测试时,builder.GetContext().Configuration
在 FunctionsStartup.Configure
中返回 null
,这当然会导致这些测试失败。我该如何解决这个问题?
我链接到的文章尚未更新以考虑 builder.GetContext().Configuration
的存在,但您可以通过一些调整使其用于测试目的。而不是使用:
var host = new HostBuilder()
.ConfigureWebJobs(new FunctionsStartup().Configure)
.Build();
您需要明确地将主机的设置复制到一个新的 WebJobsBuilderContext
中,然后将其传递给函数的启动:
var host = new HostBuilder()
.ConfigureWebJobs((context, builder) => new FunctionsStartup().Configure(new WebJobsBuilderContext
{
ApplicationRootPath = context.HostingEnvironment.ContentRootPath,
Configuration = context.Configuration,
EnvironmentName = context.HostingEnvironment.EnvironmentName,
}, builder))
.Build();
我不确定这是否是实现此目的的完全正确方法,但它对我来说效果很好。
当前版本的 Microsoft.Azure.Functions.Extensions 包公开了一个额外的 属性,使您可以轻松访问提供给函数的 IConfiguration
。以前这需要手动构建一个服务提供者,这显然是有问题的。
使用那个包我的 FunctionsStartup.cs
看起来像这样:
public override void Configure(IFunctionsHostBuilder builder)
{
base.Configure(builder);
var config = builder.GetContext().Configuration; // new in v1.1.0 of Microsoft.Azure.Functions.Extensions
var mySetting = config["MySetting"];
int.Parse(mySetting, out var mySetting);
// ... use mySetting...
}
为了测试我的 HTTP 触发函数,我使用 this article 作为基础,它详细说明了如何手动构建和启动主机来执行我的函数,就好像它是 运行在 Azure 中运行,类似于 TestServer
在 ASP.NET 中的工作方式 Core:
var host = new HostBuilder()
.ConfigureWebJobs(new FunctionsStartup().Configure)
.Build();
var functionsInstance = ActivatorUtilities.CreateInstance<MyFunctions>(host.Services);
然后我可以执行 MyFunctions
上定义的函数方法来测试它们的响应:
var request = new DefaultHttpRequest(new DefaultHttpContext());
var response = (OkObjectResult)functionsInstance.HttpTriggerMethod(request);
... assert that response is valid
问题是,当我 运行 我的测试时,builder.GetContext().Configuration
在 FunctionsStartup.Configure
中返回 null
,这当然会导致这些测试失败。我该如何解决这个问题?
我链接到的文章尚未更新以考虑 builder.GetContext().Configuration
的存在,但您可以通过一些调整使其用于测试目的。而不是使用:
var host = new HostBuilder()
.ConfigureWebJobs(new FunctionsStartup().Configure)
.Build();
您需要明确地将主机的设置复制到一个新的 WebJobsBuilderContext
中,然后将其传递给函数的启动:
var host = new HostBuilder()
.ConfigureWebJobs((context, builder) => new FunctionsStartup().Configure(new WebJobsBuilderContext
{
ApplicationRootPath = context.HostingEnvironment.ContentRootPath,
Configuration = context.Configuration,
EnvironmentName = context.HostingEnvironment.EnvironmentName,
}, builder))
.Build();
我不确定这是否是实现此目的的完全正确方法,但它对我来说效果很好。