shmish111
shmish111

Reputation: 3797

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?

Upvotes: 4

Views: 3309

Answers (2)

CShark
CShark

Reputation: 2221

This is actually actually quite easily achievable.

Please take a look at the ElasticSearch-Inside project on Github.

Essentially, this allows you to start ElasticSearch from within your integration/unit tests. This is achieved through the fact that both the Java runtime and ElasticSearch is embedded within the library's dll.

Instructions for installing the nuget package and using it within your unit tests are on the project's github page.

Upvotes: 3

Martijn Laarman
Martijn Laarman

Reputation: 13536

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

Upvotes: 7

Related Questions