简体   繁体   English

耐用的 Azure 功能和集成测试

[英]Durable Azure Functions and Integration Tests

I'm trying to test (integration tests) my Azure Durable Function v3 with SpecFlow and MSTest.我正在尝试使用 SpecFlow 和 MSTest 测试(集成测试)我的 Azure Durable Function v3。

Functions are initialized with DI and Startup class:使用 DI 和 Startup class 初始化函数:

[assembly: FunctionsStartup(typeof(Startup))]
namespace MyNamespace.Functions
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            ConfigureServices(builder.Services);
        }
...

Orchestrator entry point is triggered by HTTP endpoint: Orchestrator 入口点由 HTTP 端点触发:

public class Function1
{
    [FunctionName(nameof(Function1))]
    public async Task<IActionResult> DoYourJob(
        [HttpTrigger(AuthorizationLevel.Anonymous, methods: "post", Route = "api/routes/function1")] HttpRequestMessage httpRequest,
        [DurableClient] IDurableOrchestrationClient starter)
        {
...

My IntegrationTest constructor initializes Az Function with HostBuilder (thanks to this article ):我的 IntegrationTest 构造函数使用 HostBuilder 初始化 Az HostBuilder (感谢这篇文章):

[Binding]
public sealed class Function1StepDefinitions
{
    private readonly IHost _host;
    private readonly Function1 _function;

    public Function1StepDefinitions()
    {
        var startup = new MyNamespace.Functions.Startup();
        _host = new HostBuilder()
            .ConfigureWebJobs(config =>
            {
                config.AddDurableTask(options =>
                {
                    options.HubName = "MyTaskHub";
                });

                startup.Configure(config);
            })
            .ConfigureServices(ReplaceTestOverrides)  // Method to replace/mock some external services
            .Build();

        _function = new Function1(Host.Services.GetRequiredService<IOptions..., ..);

And in my test I dont know how to retrieve IDurableOrchestrationClient to call my HTTP Trigger:在我的测试中,我不知道如何检索IDurableOrchestrationClient来调用我的 HTTP 触发器:

    [When(@"I call my function")]
    public async Task WhenICallMyFunction()
    {
        var request = new HttpRequestMessage();

        await _function.DoYourJob(
            request,
            Host.Services.GetService<IDurableOrchestrationClient>()); // This is always null
...

Is there a way to retrieve this IDurableOrchestrationClient and test my whole Function (with Orchestrator and Activities calls)?有没有办法检索这个IDurableOrchestrationClient并测试我的整个 Function(使用 Orchestrator 和活动调用)?

Looks like there is a IDurableClientFactory that you can inject instead based on this code .看起来有一个IDurableClientFactory可以根据此代码注入。

Using this, you can create a client to use as shown in this sample .使用它,您可以创建一个要使用的客户端,如本示例中所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM