title: Using Elasticsearch with RSpec and Factorygirl
body: <p>I found a great article on integrating Elasticsearch with RSpec <a href="http://benjaminknofe.com/blog/2014/06/14/rspec-testing-rails-with-elasticsearch/" rel="nofollow">here</a>, but whenever I create a model with Factorygirl it doesn't seem to be getting added to my Elasticsearch index. Any ideas on how to add models to Elasticsearch using Factorygirl?</p>
  
  <p>Here's my code</p>
  
  <p>display_controller_spec.rb:</p>
  
  <pre><code>    it "will return a list of displays", :elasticsearch do
        FactoryGirl.create(:display, client: @client)
  
        get :index, { :format =&gt; :json }, { "Accept" =&gt; "application/json" }
        # returns Status: 200 OK
        expect(response.status).to eq 200
  
        json = JSON.parse(response.body)
        expect(json.count).to be &gt; 0
      end
  </code></pre>
  
  <p>display.rb:</p>
  
  <pre><code>    require 'elasticsearch/model'
  
      class Display &lt; ActiveRecord::Base
        include Elasticsearch::Model
        include Elasticsearch::Model::Callbacks
        index_name ['company', Rails.env, self.base_class.to_s.pluralize.underscore].join('_')
  ...
  </code></pre>
  
  <p>factories/displays.rb</p>
  
  <pre><code>FactoryGirl.define do
    sequence(:display_name) {|n| "Display ##{n}"}
  
    factory :display do
      name { generate(:display_name) }
      client
    end
  end
  </code></pre>
  
  <p>spec_helper.rb:</p>
  
  <pre><code>  config.before(:each) do
      [Event, Interaction, Perch].each do |model|
        model.__elasticsearch__.create_index!(force: true)
      end
    end
  </code></pre>