Mocking an ElasticSearch client using C# Moq Mocking an ElasticSearch client using C# Moq elasticsearch elasticsearch

Mocking an ElasticSearch client using C# Moq


The NullReferenceException occured because you didn't specify a behavior on search method. Your search method returns null and then you calls .Document on the null.

The way to specify a behavior is as the following:

elasticClientMock.Setup(x => x.Search<SegmentRecord>(                             It.IsAny</* put here the right Func */>))        .Returns( /* put here the instance you want to return */);

you have to replace my comments with the correct types.