samedi 14 octobre 2017

Using elasticsearch-extensions gem in Rails test

I'm using Elasticsearch in a Rails application and would like to be able to test the Elasticsearch code in automated tests. I've found the elasticsearch-extensions gem which allows you to spin up an Elasticsearch test cluster fairly easily. I cannot seem to actually get this running in a test though. I run the code:

Elasticsearch::Extensions::Test::Cluster.start(port: 9250, nodes: 1, timeout: 120)
client = Elasticsearch::Client.new url: 'http://localhost:9250'
client.index index: 'index', type: 'type', id: 1, body: { name: 'test' }, refresh: true

If I ran this via the Rails console or the Rails runner, I can query the Elasticsearch cluster and see that a cluster was made and an index was created. However, if I do this in a test, like so:

require 'elasticsearch/extensions/test/cluster'

class ElasticsearchTest < ActiveSupport::TestCase
  context 'running an elasticsearch cluster' do
    should 'create an index when adding a document' do
      Elasticsearch::Extensions::Test::Cluster.start(port: 9250, nodes: 1, timeout: 120)
      client = Elasticsearch::Client.new url: 'http://localhost:9250'
      client.index index: 'index', type: 'type', id: 1, body: { name: 'test' }, refresh: true
    end
  end
end

it doesn't work, and I cannot figure out why. Any ideas?

Aucun commentaire:

Enregistrer un commentaire