4

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?

shmish111
  • 3,697
  • 5
  • 30
  • 52

2 Answers2

7

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

Henrik
  • 613
  • 4
  • 11
Martijn Laarman
  • 13,476
  • 44
  • 63
  • Shortly after posting this I realized I was being a little stupid and of course it won't be possible to run an embedded .NET instance. Before I clicked on your link I thought that I should just have a test instance somewhere and create and drop indexes just as you are :) Thanks! – shmish111 Oct 21 '13 at 16:12
  • @Henrik could you update the link please? It seems to be broken – Madison Haynie May 10 '19 at 07:12
3

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.

CShark
  • 2,183
  • 1
  • 24
  • 42