ISetup<IDurableOrchestrationContext, Task<List<string>> 不包含 'ReturnsAsync' 的定义

ISetup<IDurableOrchestrationContext, Task<List<string>> does not contain a definition for 'ReturnsAsync'

我从 Durable Azure Functions(最新版本)开始,它是单元测试(使用 x-unit)。下面是我的触发函数(http 触发):

[FunctionName("funcEventSubscriber")]
public async Task<HttpResponseMessage> HttpStart(
    [HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequestMessage req,
    [DurableClient] IDurableOrchestrationClient starter,
    ILogger log)
{
    try
    {
        string instanceId = await starter.StartNewAsync("funcEventOrchestrator", null);
        log.LogInformation($"Started orchestration with ID = '{instanceId}'.");
        return starter.CreateCheckStatusResponse(req, instanceId);
    }
    catch (System.Exception ex)
    {
        _telemetryHelper.LogException(ex);
        throw;
    }
}

这是我的协调器功能,它启动了 activity 功能:

[FunctionName("funcEventOrchestrator")]
public async Task<List<string>> RunOrchestrator(
    [OrchestrationTrigger] IDurableOrchestrationContext context)
{
    try
    {
        var outputs = new List<string>();
        var reusultSet = new List<string>();
        outputs = await context.CallActivityAsync<List<string>>("funcActivityA", "");
        foreach (var item in outputs)
        {
            var result = await context.CallActivityAsync<List<TableRegionEntity>>("funcActivityB", item);
            if (result.Count > 0)
            {
                foreach (var data in result)
                {
                    List<string> inputObject = new List<string>();
                    inputObject.Add(data.Subscriber);
                    inputObject.Add(item);
                    reusultSet.Add(await context.CallActivityAsync<string>("funcActivityC", inputObject));
                }
            }
            else
            {
                reusultSet.Add(await context.CallActivityAsync<string>("funcActivityD", item));
            }
        }
        return reusultSet;
    }
    catch (System.Exception ex)
    {
        _telemetryHelper.LogException(ex);
        throw;
    }
}

我为协调器函数编写的单元测试方法如下所示:

[Fact]
public async Task Run_returns_list_of_string()
{
    var mockContext = new Mock<IDurableOrchestrationContext>();
    mockContext.Setup(x => x.CallActivityAsync<List<string>>("funcActivityA", "")).ReturnsAsync(funcActivityAOutput);
    mockContext.Setup(x => x.CallActivityAsync<List<TableRegionEntity>>("funcActivityB", inputJsonString)).ReturnsAsync(funcActivityBOutput);
    mockContext.Setup(x => x.CallActivityAsync<string>("funcActivityC", funcActivityCInputObject)).ReturnsAsync(funcActivityCOutput);
    mockContext.Setup(x => x.CallActivityAsync<string>("funcActivityD", inputJsonString)).ReturnsAsync(funcActivityDOutput);

    // Instantiate funcEventSubscriber
    funcEventSubscriber funcEventSubscriberObj = new funcEventSubscriber(telemetryHelperMock.Object);

    // Act
    var result = await funcEventSubscriberObj.RunOrchestrator(mockContext.Object); // call orchestrator fn

    // Assert
    Assert.Equal(3, result.Count);
}

我设置 mockContext 的方式会引发错误。这是 mockContext.Setup 第一行的错误,即

mockContext.Setup(x => x.CallActivityAsync<List<string>>("funcActivityA", "")).ReturnsAsync(funcActivityAOutput);`

我哪里错了?

在您的设置中,考虑到模拟成员被定义为

,所讨论的行假设为 return a List<string>

IDurableOrchestrationContext.CallActivityAsync<TResult>(String, Object) Method

public Task<TResult> CallActivityAsync<TResult> (string functionName, object input);

其中

TResult: The return type of the scheduled activity function.

确保模拟成员使用了正确的 return 类型。这样就可以在设置

上调用正确的 ReturnsAsync 扩展

例如

//...

List<string> funcActivityAOutput = new List<string>() {
    //...populate list as needed
}
var mockContext = new Mock<IDurableOrchestrationContext>();
mockContext
    .Setup(x => x.CallActivityAsync<List<string>>("funcActivityA", ""))
    .ReturnsAsync(funcActivityAOutput);

//...