I'm having problems creating Unit test for the search using ElasticSearch with Nest.
Unit Test
var mockSearchResponse = new Mock<ISearchResponse<Person>>();
mockSearchResponse.Setup(x => x.Documents).Returns(_people);
var mockElasticClient = new Mock<IElasticClient>();
mockElasticClient.Setup(x => x.Search(It.IsAny<Func<SearchDescriptor<Person>, SearchRequest<Person>>>())).Returns(mockSearchResponse.Object);
var service = new PersonService(mockElasticClient.Object);
var result = service.Search(string.Empty, string.Empty);
Assert.AreEqual(2,result.Count());
Working code
results = ConnectionClient.Search<Person>(s => s.Index("person_index").Query(q => q.Term(t => t.Id, searchValue))).Documents;
The result is always null, even if I do the following
var temp = ConnectionClient.Search<Person>(s => s.Index("person_index").Query(q => q.Term(t => t.Id, searchValue)));
Any help would be appreciated.
The signature of the
Func<T1, T2>
passed toIt.IsAny<T>()
is not correct so the setup expectation will never be matched. The signature should beA full working example
If you don't need to stub the client then you can simply use a real client and set the
IConnection
to an instance ofInMemoryConnection
This way you could also capture the requests if you needed to. You could take this a step further and create your own
IConnection
implementation that returns stub responses.