Ruby on Rails - Sphinx不会在Rspec测试中索引数据

Exi*_*iRe 2 sphinx thinking-sphinx ruby-on-rails-3.1

我使用sphinx并思考sphinx在我的Ruby on Rails应用程序中搜索数据.所以,我有这个测试来检查我的工作:

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
Run Code Online (Sandbox Code Playgroud)

问题是 - ThinkingSphinx::Test.index不起作用.为什么?

sphinx.yml

development:
    port: 9312
    ...

test:
    port: 9313
    ...
Run Code Online (Sandbox Code Playgroud)

我的系统:

Mac OS X 
PostgreSQL 9 
Rails 3.1.3 
Ruby 1.9.3
Sphinx 2.0.3-release (r3043)
Run Code Online (Sandbox Code Playgroud)

pat*_*pat 5

你在RSpec上使用交易设备吗?因为Sphinx无法访问RSpec上下文之外未保存在数据库中的记录.此外,在索引Sphinx以赶上索引数据后,您应该允许四分之一秒或半秒:

sleep(0.25)
Run Code Online (Sandbox Code Playgroud)

总而言之,我建议在控制器测试中删除Sphinx,并且只在集成测试中运行Sphinx(通过cucumber/capybara或其他方式).