# How to mock Elasticsearch ExistsAsync response

**URL:** <https://discuss.elastic.co/t/how-to-mock-elasticsearch-existsasync-response/371935>\
**Category:** Elasticsearch\
**Created:** [December 12, 2024, 8:46pm UTC](https://discuss.elastic.co/t/how-to-mock-elasticsearch-existsasync-response/371935 "2024-12-12T20:46:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guthula\_Baladitya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guthula_baladitya/32/139968_2.png) [@Guthula\_Baladitya](https://discuss.elastic.co/u/Guthula_Baladitya)\
**Post date:** [December 12, 2024, 8:46pm UTC](https://discuss.elastic.co/t/how-to-mock-elasticsearch-existsasync-response/371935/1 "2024-12-12T20:46:44Z")

</div>

Hello everyone,

I have a piece of code in my repository class where I'm checking whether a specific index exists or not.

```auto
var IndexExists = await _elasticClient.Indices.ExistsAsync(IndexName);
            if ((await _elasticClient.Indices.ExistsAsync(IndexName)).Exists)
            {
                datasetsToQuery.Add(IndexName);
            }

```

I'm writing unit test cases using NSubstitute to mock the `ExistsAsync` response, but I'm encountering issues when trying to use the `Exists` method. Is there a way to work around this or an alternative approach to mock the `ExistsAsync` method?

```auto
var response = TestableResponseFactory.CreateResponse(
            new ExistsResponse
            {
               Exists = true
            },
            200, // HTTP 200 OK
            true // Valid response flag
          );

// Mock ExistsAsync for Indices and CancellationToken parameters
          _elasticClient
            .ExistsAsync(
              Arg.Any<Elastic.Clients.Elasticsearch.Indices>(), // Match any Indices argument
              Arg.Any<System.Threading.CancellationToken>() // Match any CancellationToken argument
            )
            .Returns(Task.FromResult(response)); 

```

Here Exists is giving me a compliation error saying "A property without setter or inaccessible setter cannot be assigned to"

Thank you.

---

<div class="post-metadata">

**Author:** ![flobernd](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flobernd/32/124877_2.png) [@flobernd](https://discuss.elastic.co/u/flobernd)\
**Post date:** [December 13, 2024, 8:51am UTC](https://discuss.elastic.co/t/how-to-mock-elasticsearch-existsasync-response/371935/2 "2024-12-13T08:51:39Z")

</div>

Hi @Guthula_Baladitya ,

please see my response here:

> [@Mocking the .NET Client for ExistsResponse in unit tests](https://discuss.elastic.co/t/mocking-the-net-client-for-existsresponse-in-unit-tests/371915):
>
> Hello Everyone, I am using .NET 8 and "Elastic.Clients.Elasticsearch" Version="8.15.6" (not NEST) For mocking we are using NSubstitute(not Moq) I tried to go through multiple blogs and even the github repo for .NET client tests but could not find anything concrete around how to mock the sealed class for exists response i.e. public sealed partial class ExistsResponse : ElasticsearchResponse We could successfully use it for SearchResponse and MappingResponse but could not make it work for Exis…
