簡體   English   中英

耐用的 Azure 功能和集成測試

[英]Durable Azure Functions and Integration Tests

我正在嘗試使用 SpecFlow 和 MSTest 測試(集成測試)我的 Azure Durable Function v3。

使用 DI 和 Startup class 初始化函數:

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

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)
        {
...

我的 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..., ..);

在我的測試中,我不知道如何檢索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
...

有沒有辦法檢索這個IDurableOrchestrationClient並測試我的整個 Function(使用 Orchestrator 和活動調用)?

看起來有一個IDurableClientFactory可以根據此代碼注入。

使用它,您可以創建一個要使用的客戶端,如本示例中所示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM