无法将异步Lambda表达式转换为委托

时间:2018-08-17 09:00:11

标签: c# unit-testing moq

我正在为MS Bot对话框设置单元测试,但遇到以下错误,不确定如何解决。有人可以帮忙吗?谢谢

  

错误CS4010无法将异步lambda表达式转换为委托类型'Task<HttpOperationResponse<BotData>>'。异步lambda表达式可能会返回void,Task或Task<T>,它们都不能转换为'Task<HttpOperationResponse<BotData>>'。   D3DirectlineBot.UnitTests C:\ Repos \ D3DirectlineBot \ D3DirectlineBot.UnitTests \ MockConnectorFactory.cs 109有效

模拟课程

public Mock<StateClient> MockIBots(MockConnectorFactory mockConnectorFactory)
{
    var botsClient = new Moq.Mock<StateClient>(MockBehavior.Loose);

    botsClient.Setup(d => d.BotState.SetConversationDataWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<BotData>(), It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>()))
        .Returns<string, string, BotData, Dictionary<string, List<string>>, CancellationToken>(async (channelId, conversationId, data, headers, token) =>
        {
            return await mockConnectorFactory.UpsertData(channelId, null, conversationId, BotStoreType.BotConversationData, data);
        });

        return botsClient;
    }

UpsertData类

protected async Task<HttpOperationResponse<object>> UpsertData(string channelId, string userId, string conversationId, BotStoreType storeType, BotData data)
{
    var _result = new HttpOperationResponse<object>();
    _result.Request = new HttpRequestMessage();
    try
    {
        var address = AddressFrom(channelId, userId, conversationId);
        await memoryDataStore.SaveAsync(address, storeType, data, CancellationToken.None);
    }
    catch (HttpException e)
    {
        _result.Body = e.Data;
        _result.Response = new HttpResponseMessage { StatusCode = HttpStatusCode.PreconditionFailed };
        return _result;
    }
    catch (Exception)
    {
        _result.Response = new HttpResponseMessage { StatusCode = HttpStatusCode.InternalServerError };
        return _result;
    }
    _result.Body = data;
    _result.Response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
    return _result;
}

2 个答案:

答案 0 :(得分:0)

您应该只返回Task而不必等待它:

return mockConnectorFactory.UpsertData(channelId, null, conversationId, BotStoreType.BotConversationData, data);

答案 1 :(得分:0)

UpsertData类:将HttpOperationResponse<object>更改为HttpOperationResponse<Botdata>

protected async Task<HttpOperationResponse<Botdata>> UpsertData(string channelId, string userId, string conversationId, BotStoreType storeType, BotData data)
{
    var _result = new HttpOperationResponse<Botdata>();
    _result.Request = new HttpRequestMessage();
    try
    {
        var address = AddressFrom(channelId, userId, conversationId);
        await memoryDataStore.SaveAsync(address, storeType, data, CancellationToken.None);
    }
    catch (HttpException e)
    {
        _result.Body = data;
        _result.Response = new HttpResponseMessage { StatusCode = HttpStatusCode.PreconditionFailed };
        return _result;
    }
    catch (Exception)
    {
        _result.Response = new HttpResponseMessage { StatusCode = HttpStatusCode.InternalServerError };
        return _result;
    }
    _result.Body = data;
    _result.Response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
    return _result;
}

MockConnectionFactory类:已删除@DmitryBogatykh提到的asyncawait

    botsClient.Setup(d => d.BotState.SetConversationDataWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<BotData>(), It.IsAny<Dictionary<string, List<string>>>(), It.IsAny<CancellationToken>()))
        .Returns((string channelId, string conversationId, BotData data, Dictionary<string, List<string>> headers, CancellationToken token) =>
        {
            return  mockConnectorFactory.UpsertData(channelId, null, conversationId, BotStoreType.BotConversationData, data);
        });