Mocking the .net client

In 8.0 Release notes it mentions:
" In order to support user testing scenarios, we have unsealed the ElasticsearchClient type and made its methods virtual. This supports mocking the type directly for unit testing."

I haven't been able to find examples of this. Does someone have an example of mocking the ElasticsearchClient?

Hi, @samlane.

We're working on some documentation around this, but at a high-level it looks something like this:

var createResponse = new CreateIndexResponse { Acknowledged = true, Index = "testing", ShardsAcknowledged = true };
var mockedResponse = TestableResponseFactory.CreateSuccessfulResponse(createResponse, 201);

var mockedClient = Mock.Of<ElasticsearchClient>(e =>
	e.Indices.Create<It.IsAnyType>() == mockedResponse);

var testResponse = mockedClient.Indices.Create<Person>();

if (testResponse.IsValidResponse)
	Console.WriteLine("SUCCESS");

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.