How do I mock the NestClient DebugInformation in C#?

Viewed 99

I'd like to Moq the response from the ElasticClient to show an error or some information in the DebugInformation property.

I have the following:

ISearchResponse<person> personResponse = new SearchResponse<person>();
personResponse.ApiCall = new ApiCallDetails
{
    Success = false,
};
personResponse.DebugInformation = "Something happened"; //This is not allowed

_elasticClient.Setup(s =>
    s.Search<person>(
        It.IsAny<Func<SearchDescriptor<person>, ISearchRequest>>()))
    .Returns(personResponse);

I am unable to assign to DebugInformation as it is readonly. Any ideas?

1 Answers

Someone suggested I Moq the Interface of the response instead

So I mocked it Like this

Mock<ISearchResponse<person>> mockResponse = new Mock<ISearchResponse<person>>();
mockResponse.Setup(s => s.ApiCall.DebugInformation).Returns("Something");
mockResponse.Setup(s => s.ApiCall.Success).Returns(false);
mockResponse.Setup(s => s.Hits).Returns(new List<IHit<person>>());


_elasticClient.Setup(s =>
    s.Search<person>(
        It.IsAny<Func<SearchDescriptor<person>, ISearchRequest>>()))
    .Returns(mockResponse.Object);

... and this works!

Related