Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write a request spec with Capybara/RSpec for testing Sunspot/Solr searching?

I'd like to write my usual RSpec/Capybara request specs to test search functionality using Sunspot and Solr. I've been digging around but can't find how to get this working. I have the sunspot_test gem installed and have verified that the Products created do exist. The issue seems to be with the indexing, maybe? What am I missing?

require 'spec_helper'

describe "search" do

  context "when searching by name/description" do

    let!(:super_mario_bros_3) { Factory(:product, :name => 'Super Mario Bros. 3') }
    let!(:legend_of_zelda)    { Factory(:product, :name => 'Legend of Zelda') }

    before { Product.reindex; Sunspot.commit }


    it "should only find games matching the search text", :js => true, :search => true do
      # search_for fills in and submits the search form
      search_for("Super")

      # This yields an empty array
      p Product.search { keyword "super" }.results

      # These fail
      page.should have_content super_mario_bros_3.name
      page.should have_no_content legend_of_zelda.name
    end

  end

end
like image 944
Eric M. Avatar asked Aug 08 '11 21:08

Eric M.


1 Answers

You're probably making the same mistake that I did. See the answer to my post here - Sunspot and RSpec fail. The commit doesn't seem to be working

Any test which uses Sunspot should be as follows....

describe "search", :search => true do

and make sure you have the following in your spec_helper.rb

require 'sunspot_test/rspec'
like image 149
Simmo Avatar answered Oct 09 '22 09:10

Simmo