2012-02-02 58 views
2

我使用sphinx和思维狮身人面像来在我的Ruby on Rails应用程序中搜索数据。所以,我有这个测试来检查我的工作:Ruby on Rails - Sphinx不会在Rspec测试中索引数据

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) 

回答

5

是否使用事务灯具使用RSpec?因为Sphinx无法访问RSpec上下文以外未保存在数据库中的记录。此外,你应该允许一个季度索引后半秒的狮身人面像与索引数据赶超:

sleep(0.25) 

所有这一切说,我会在你的控制器测试建议删空狮身人面像,只有内运行狮身人面像整合测试(通过黄瓜/水豚或其他)。

+0

感谢您提供'sleep'提示,这让我疯狂。 – declan 2012-10-11 20:03:52