Integration Testing can be quite cumbersome, especially when you have a lot of moving parts involved. To test my ElasticSearch code I was used to spin up a docker instance and discard it after the tests ran.
Recently I changed my approach after receiving the following tip from a colleague(thanks Jasper!): https://github.com/poulfoged/elasticsearch-inside
ElasticSearch Inside is a fully embedded version of Elasticsearch for integration tests. When the instance is created both the JVM and Elasticsearch itself is extracted to a temporary location and started. Once disposed everything is removed again. And despite what you may think, this happens really fast(a few seconds on my machine).
How to
- Install the Nuget package:
Install-Package elasticsearch-inside
- After that you have to create a new instance of the ElasticSearch class and wait for the Ready() method.
using (var elasticsearch = new Elasticsearch()) { await elasticsearch.Ready(); }
- Now you can call the ElasticSearch instance using NEST:
using (var elasticsearch = new Elasticsearch()) { await elasticsearch.Ready(); var client = new ElasticClient(new ConnectionSettings(elasticsearch.Url)); var result = client.Ping(); }