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

时间:2012-02-02 16:25:04

标签: ruby-on-rails-3.1 sphinx thinking-sphinx

我使用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

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

sphinx.yml

development:
    port: 9312
    ...

test:
    port: 9313
    ...

我的系统:

Mac OS X 
PostgreSQL 9 
Rails 3.1.3 
Ruby 1.9.3
Sphinx 2.0.3-release (r3043)

1 个答案:

答案 0 :(得分:5)

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

sleep(0.25)

所有这一切,我建议在你的控制器测试中删除Sphinx,并且只在集成测试中运行Sphinx(通过cucumber / capybara或其他方式)。