Testing Elasticsearch In Your Rails 4 Application With RSpec

February 10, 2015

What is Elasticsearch?

Elasticsearch is an open-source real-time search and analytics engine that runs on top of Lucene, a Java-based indexing and search library.

If you haven’t setup elasticsearch with your Rails application, you can read about how to do it in these articles:

Steps to setup your RSpec tests

Step 1: Configure spec_helper.rb with a truncation strategy

Below is a copy of my spec_helper.rb setup. The important part is the

config.around :each, elasticsearch: true



Step 2: Here’s a snapshot of my database_cleaner.rb

Below is a copy of my database_cleaner.rb setup.


Step 3: Use a tag to denote your elasticsearch tests

Below is a gist copy of a post_specs.rb file. Note that I’m calling the “posts” action of the API controller (Posts) which then runs a search against the Post model using ElasticSearch.

Note the use of the elasticsearch and commit tags. I’m also calling

sleep to allow a delay so the test index has time to get built. I

don’t like this solution, but for the moment it’s working and I’ll

update this post if I find a better way.


Profile picture

Written by Bruce Park who lives and works in the USA building useful things. He is sometimes around on Twitter.