# Elasticsearch C# Nest unit tests with Moq : fail to mock concrete class

**URL:** <https://discuss.elastic.co/t/elasticsearch-c-nest-unit-tests-with-moq-fail-to-mock-concrete-class/274657>\
**Category:** Elasticsearch\
**Created:** [June 1, 2021, 3:06pm UTC](https://discuss.elastic.co/t/elasticsearch-c-nest-unit-tests-with-moq-fail-to-mock-concrete-class/274657 "2021-06-01T15:06:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicolas\_Rey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicolas_rey/32/47284_2.png) [@Nicolas\_Rey](https://discuss.elastic.co/u/Nicolas_Rey)\
**Post date:** [June 1, 2021, 3:06pm UTC](https://discuss.elastic.co/t/elasticsearch-c-nest-unit-tests-with-moq-fail-to-mock-concrete-class/274657/1 "2021-06-01T15:06:16Z")

</div>

Hi,

I need to mock some methods and properties in `Nest.IElasticClient` interface that is used in my repo. As example I need to stub the `Indices.Exists()` method to return an `ExistsResponse` having an `Exists` property returning true.

The problem is that the concrete class has no interface implementation, nor setter on the Exists property, and is not declared virtual neither in Nest lib:

```auto
public class ExistsResponse : ResponseBase
{
    public ExistsResponse();
    public bool Exists { get; }
}

public ExistsResponse Exists(Indices index, Func<IndexExistsDescriptor, IIndexExistsRequest> selector = null);

```

So for the mocking I tried to set the property anyway on the concrete class, but it failed with all the following methods, I have no idea on how to do ...

```auto
/* Fail with exception :
System.NotSupportedException : Unsupported expression: x => x.Exists
    Non-overridable members (here: ExistsResponse.get_Exists) may not be used in setup / verification expressions.
*/
    var mock1 = new Mock<ExistsResponse>();
    obj.SetupGet(f => f.Exists).Returns(true);

/* Fail with exception :
 System.NotSupportedException : Unsupported expression: f => f.Exists
    Non-overridable members (here: ExistsResponse.get_Exists) may not be used in setup / verification expressions.
*/
    var mock2 = Mock.Of<ExistsResponse>(x => x.Exists == true);

/* Fail with exception :
System.ArgumentException : Property set method not found.
*/
    var mock3 = new ExistsResponse();
    var property = typeof(ExistsResponse).GetProperty("Exists", BindingFlags.Public | BindingFlags.Instance);
    property.SetValue(mock3, true);

/* Fail with exception :
System.NullReferenceException (setter is null)
*/
    var mock4 = new ExistsResponse();
    var setter = property.GetSetMethod(true);
    setter.Invoke(mock4, new object[] { true });

    // My Mock on the Indices.Exists method
    var elasticMock = new Mock<IElasticClient>();
    elasticMock
    .Setup(x => x.Indices.Exists(It.IsAny<string>(), null))
    .Returns(/ ***my stubbed object here*** /); // <== how to stub it to return a ExistsResponse.Exists = false ?

```

My libs

```auto
Nest 7.12.1
Moq 4.15.2
XUnit 2.4.1
.Net 5

```

Thank you for your help

---

<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:** [June 29, 2021, 3:07pm UTC](https://discuss.elastic.co/t/elasticsearch-c-nest-unit-tests-with-moq-fail-to-mock-concrete-class/274657/2 "2021-06-29T15:07:01Z")

</div>

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