Я пытаюсь получить один документ из моего экземпляра ElasticSearch, используя GetAsync
. Я делаю что-то вроде этого
var document= await client.GetAsync<MyDocument>("documentId");
return document.Source;
Я бы хотел сделать этот вызов в модульном тесте. Я пытался сделать что-то вроде этого
[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);
// ...
}
Но когда я делаю это, экземпляр mockClient
дает эту ошибку
'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)
Я сбит с толку битом SequenceExtensions
в ошибка, поскольку я не использую SetupSequence
(если это то, на что он ссылается). Я попытался изменить тип responseMock
на GetResponse<ElasticHitContainer>
, и это избавило от ошибки компиляции, но теперь я не могу правильно смоделировать возвращаемое значение Source
, так как я не использую интерфейс.
Кто-нибудь знает, как я могу обойти это?