Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

testing elasticsearch on .NET

With Java I can write tests against an embedded elasticsearch node, this gives me loads of testing possibilities such as testing index config and tokenizers however more importantly I can test my search services with functional, easy to read and effective tests, no mocking of the client and dealing with query builders and responses in my tests etc. How can I do this in .NET?

like image 555
shmish111 Avatar asked Dec 25 '22 18:12

shmish111


1 Answers

You can't run in embedded mode with .NET you will have to speak with an elasticsearch server somewhere.

Using nest you can easily talk to a different index specifically for testing i.e

var uri = new Uri("http://localhost:9200");
var connectionSettings = new ConnectionSettings(uri, "my-test-index");
var client = new ElasticClient(connectionSettings);

my-test-index will now be used as index for every call which doesn't explicitly specify one. Depending on how invasive your tests are you could even create an index suffixed with a guid and delete the index after every test run.

This is also the approach NEST itself takes when running integration tests: https://github.com/elastic/elasticsearch-net/blob/develop/src/Tests/Nest.Tests.Integration/IntegrationSetup.cs

like image 111
Martijn Laarman Avatar answered Jan 02 '23 16:01

Martijn Laarman