How to mock Elasticsearch ExistsAsync response

Hello everyone,

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

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?

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.

Hi @Guthula_Baladitya ,

please see my response here: