How to mock ElasticSearch NEST's GetAsync

Viewed 633

I'm trying to get one document from my ElasticSearch instance using GetAsync. I do it something like this

var document= await client.GetAsync<MyDocument>("documentId");
return document.Source;

I'd like to mock out this call in a unit test. I tried doing something like this

[Fact]
public async Task TestGetDocument_ExpectSuccess()
{
    var mockDocument = new Mock<IGetResponse<MyDocument>>(MockBehavior.Strict);
        mockDocument
            .Setup(r => r.Source)
            .Returns(_myDocument);
    var mockClient = new Mock<IElasticClient>(MockBehavior.Strict);
        mockClient
            .Setup(client => client.GetAsync(
                _documentId,
                It.IsAny<Func<GetDescriptor<MyDocument>, IGetRequest>>(),
                It.IsAny<CancellationToken>()))
            .ReturnsAsync(mockDocument.Object);
    // ...
}

But when I do that, the instantiation of mockClient gives this error

'ISetup<IElasticClient, Task<GetResponse<MyDocument>>>' does not contain a definition for 'ReturnsAsync' and the best extension method overload 'SequenceExtensions.ReturnsAsync<IGetResponse<MyDocument>>(ISetupSequentialResult<Task<IGetResponse<MyDocument>>>, IGetResponse<MyDocument>)' requires a receiver of type 'ISetupSequentialResult<Task<IGetResponse<MyDocument>>>' [Foo.Bar.ElasticSearch.UnitTests]csharp(CS1929)

I'm confused by the SequenceExtensions bit in the error since I'm not using SetupSequence (if that's what it is referring to). I tried changing the responseMock type to GetResponse<ElasticHitContainer>, and it gets rid of the compile error, but now I cannot properly mock out the return value of Source since I'm not using an interface.

Anyone know how I can get around this?

0 Answers
Related