【问题标题】:Ruby on Rails - Sphinx doesn't index data in Rspec testsRuby on Rails - Sphinx 不在 Rspec 测试中索引数据
【发布时间】:2012-02-02 16:25:04
【问题描述】:

我使用 sphinx 和 thinking 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)

【问题讨论】:

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


    【解决方案1】:

    您是否在 RSpec 中使用事务性固定装置?因为 Sphinx 无法访问未保存在 RSpec 上下文之外的数据库中的记录。此外,您应该在为 Sphinx 编制索引后留出四分之一或半秒的时间来赶上索引数据:

    sleep(0.25)
    

    综上所述,我建议在控制器测试中删除 Sphinx,并且只在集成测试中运行 Sphinx(通过 cucumber/capybara 或其他方式)。

    【讨论】:

    • 感谢sleep 的提示,这让我发疯了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 2016-01-25
    相关资源
    最近更新 更多