# Mocking the .net client

**URL:** <https://discuss.elastic.co/t/mocking-the-net-client/330147>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [April 17, 2023, 3:13pm UTC](https://discuss.elastic.co/t/mocking-the-net-client/330147 "2023-04-17T15:13:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![samlane](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@samlane](https://discuss.elastic.co/u/samlane)\
**Post date:** [April 17, 2023, 3:13pm UTC](https://discuss.elastic.co/t/mocking-the-net-client/330147/1 "2023-04-17T15:13:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![stevejgordon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stevejgordon/32/80912_2.png) [@stevejgordon](https://discuss.elastic.co/u/stevejgordon)\
**Post date:** [April 26, 2023, 9:42am UTC](https://discuss.elastic.co/t/mocking-the-net-client/330147/2 "2023-04-26T09:42:23Z")

</div>

Hi, @samlane.

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

```auto
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");

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 24, 2023, 9:42am UTC](https://discuss.elastic.co/t/mocking-the-net-client/330147/3 "2023-05-24T09:42:59Z")

</div>

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