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:
- How To Install Elasticsearch On Linux for Rails Development
- How To Setup Elasticsearch In Your Rails Application In Development
- How to Setup Elasticsearch in your Rails App in Production
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.