samedi 14 novembre 2020

Mock GetAsync of IElasticClient. Error: Convert IGetResponse<> to GetResponse<>

everyone.

I have been trying write mock test to my IElasticClient calls, but I have problem with a error about convert. Convert IGetResponse to GetResponse.

My method of ElasicClient:

public class GetClientByIdQueryHandler : IQueryHandler<GetClientByIdQuery, Client>
{
    private readonly IElasticClient _elasticClient;
    private readonly IReadModel _readModel;

    public GetProposalByIdQueryHandler(IElasticClient elasticClient, IReadModel readModel)
    {
        ...
    }

    public async Task<Client> ExecuteQueryAsync(GetClientByIdQuery query, CancellationToken cancellationToken)
    {
        var readModelDescription = _readModel.GetReadModelDescription<ClientReadModel>();
        var indexName = readModelDescription.IndexName.Value;

        var getResponse = await _elasticClient
            .GetAsync<ClientReadModel>(query.Id.Value, d => d.Index(indexName).RequestConfiguration(c => c.AllowedStatusCodes((int)HttpStatusCode.NotFound)), cancellationToken)
            .ConfigureAwait(false);

        return getResponse.Source;
    }
}

My Mock test:

    private const string _indexName = "client-document";

    private ClientReadModel GetClientReadModel()
    {
        var response = JsonConvert.DeserializeObject<ClientReadModel>("...string json...");
        
        return response;
    }

    [Fact]
    public async Task GetClientByIdQueryHandler()
    {
        // Arrange
        var mockElasticClient = new Mock<IElasticClient>();

        var mockResponse = new Mock<IGetResponse<ClientReadModel>>();
        mockResponse.SetupGet(r => r.Source).Returns(GetClientReadModel());

        mockElasticClient
            .Setup(x => x.GetAsync(
                "id-d862975d-06ea-4f50-b7d3-d2b19413cb4c",
                It.IsAny<Func<GetDescriptor<ClientReadModel>, IGetRequest>>(),
                It.IsAny<CancellationToken>()))
            .Returns(mockResponse.Object); // <== error here

        ...
        ...
    }

Almost alright, but it returns a error on the line "Returns(mockResponse.Object);":

cannot convert from 'Nest.IGetResponse' to 'Nest.GetResponse

Someone have idea about resolve this problem?

Thanks!

Aucun commentaire:

Enregistrer un commentaire