2

I use sphinx and thinking sphinx to search data in my Ruby on Rails application. So, i have this test to check my work:

require 'spec_helper'
require 'thinking_sphinx/test'

describe SearchesController do
  render_views

  #Start search server in test mode
  before(:all) do
      ThinkingSphinx::Test.init
      ThinkingSphinx::Test.start
  end

    describe "when signed in" do
      before(:each) do
        @user = test_sign_in( Factory( :user ) )
        @micropost = Factory( :micropost,
                              :user => @user,
                              :content => "Test message of user")
        ThinkingSphinx::Test.index
        get :find_microposts, :q => @micropost.content                        #Sending data (by :q => "Text")
      end

      it "should find micropost of user" do
        response.should have_selector( "table.microposts",
                                       :content => @micropost.content)
      end
    end
  end


  #Stop search server in test mode
  after(:all) do
      ThinkingSphinx::Test.stop
  end
end

Problem is - ThinkingSphinx::Test.index doesn't work. Why?

sphinx.yml

development:
    port: 9312
    ...

test:
    port: 9313
    ...

My system:

Mac OS X 
PostgreSQL 9 
Rails 3.1.3 
Ruby 1.9.3
Sphinx 2.0.3-release (r3043)
ExiRe
  • 4,727
  • 7
  • 47
  • 92

1 Answers1

5

Are you using transactional fixtures with RSpec? Because Sphinx can't access records that aren't saved in the database outside of the RSpec context. Also, you should allow a quarter or half a second after indexing for Sphinx to catch up with the indexed data:

sleep(0.25)

All that said, I would recommend stubbing out Sphinx in your controller tests, and only running Sphinx within integration tests (via cucumber/capybara or otherwise).

pat
  • 16,116
  • 5
  • 40
  • 46