ElasticSearch 2.0 Nest Unit Testing with MOQ
The signature of the Func<T1, T2>
passed to It.IsAny<T>()
is not correct so the setup expectation will never be matched. The signature should be
It.IsAny<Func<SearchDescriptor<Person>, ISearchRequest>>()
A full working example
void Main(){ var people = new List<Person> { new Person { Id = 1 }, new Person { Id = 2 }, }; 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>, ISearchRequest>>())) .Returns(mockSearchResponse.Object); var result = mockElasticClient.Object.Search<Person>(s => s); Assert.AreEqual(2, result.Documents.Count()).Dump();}public class Person{ public int Id { get; set;}}
If you don't need to stub the client then you can simply use a real client and set the IConnection
to an instance of InMemoryConnection
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));// pass an instance of InMemoryConnection so that requests are not // **actually** sentvar connectionSettings = new ConnectionSettings(pool, new InMemoryConnection()) .PrettyJson() .DisableDirectStreaming() .OnRequestCompleted(response => { // log out the request if (response.RequestBodyInBytes != null) { Console.WriteLine( $"{response.HttpMethod} {response.Uri} \n" + $"{Encoding.UTF8.GetString(response.RequestBodyInBytes)}"); } else { Console.WriteLine($"{response.HttpMethod} {response.Uri}"); } // log out the response if (response.ResponseBodyInBytes != null) { Console.WriteLine($"Status: {response.HttpStatusCode}\n" + $"{Encoding.UTF8.GetString(response.ResponseBodyInBytes)}\n" + $"{new string('-', 30)}\n"); } else { Console.WriteLine($"Status: {response.HttpStatusCode}\n" + $"{new string('-', 30)}\n"); } });var client = new ElasticClient(connectionSettings);
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.