Wednesday 1 August 2012

Stub responses with Nest.ElasticClient

We've started using ElasticSearch at work for some of our projects. When we started out doing simple web requests was easy enough but as the complexity of what we where doing grew it became obvious that we where starting to write our own DSL for elastic search. Surely someone else has already done this?

There are several options but we settled on NEST. Mainly because of the way the DSL was written. The first stumbling block I hit was unit testing. You could just stub out IElasticClient and then add some integration tests to cover yourself.

The problem with integration tests is they're slow and require, well something to integrate with, so I thought it would be good to stub the json response from ElasticSearch.

When using NEST it's not all that obvious. But here's how I did it:


The down side is you're testing the frameworks assumptions. But it's good to know what the framework is actually doing under the hood i.e. calling a web request with some parameters.

You also get the added bonus of being able to test that the response can be parsed into you're objects.

Doing to much in one test?

Absolutely, in our code base it's two tests. One for parsing the response and another test to assert post parameters.