ThinkingSphinx在RSpec中不起作用

时间:2012-09-11 15:15:15

标签: ruby ruby-on-rails-3 rspec thinking-sphinx

我已经阅读过帖子,说这是由于交易固定装置,但看起来我们没有使用它们。我已经阅读了其他帖子,建议我在每次测试运行之前做一些时髦的停止,重新启动和重新索引。这似乎没有帮助。

ThinkingSphinx在应用程序中运行得很好,但在测试中它表现得非常奇怪。在我的规范中创建了几个Organization模型后,我运行了一个sphinx搜索。它有两个我期待的记录,但也有很多。这很烦人,但是我添加了一个:retry_stale => true并且那些消失了,所以我对第一个查询感到满意:

(rdb:1) p Organization.search '', :retry_stale => true
[#<Organization id: 1, ein: nil, name: "Allina", created_at: "2012-09-11 15:03:04", updated_at: "2012-09-11 15:03:04", parent_id: nil, main_phone: "7867685187x894", fax: nil, email: nil, url: nil, crm_id: 1, target_market: false, notes: nil, form990_notes: nil, mec_revenue: 750000.0, legacy_id: nil, teaching_program_type_id: nil, sponsorship_type_id: nil, is_member_council_teaching_hospitals: nil, has_teaching_program: nil, is_major_academic_medical_center: nil, does_participate_in_survey: nil, status: "modified", net_revenue: nil, gross_revenue: nil, source_name: nil, source_id: nil, import_concat_key: nil, bigtime_id: nil, is_non_hc: nil, is_client: nil, contact_record_id: nil, form990_legacy_id: nil, number_of_beds: nil, tax_exempt_status: nil, delta: true>, #<Organization id: 2, ein: nil, name: "HealthPartners", created_at: "2012-09-11 15:03:04", updated_at: "2012-09-11 15:03:04", parent_id: nil, main_phone: "3407862693x0935", fax: nil, email: nil, url: nil, crm_id: 1, target_market: false, notes: nil, form990_notes: nil, mec_revenue: nil, legacy_id: nil, teaching_program_type_id: nil, sponsorship_type_id: nil, is_member_council_teaching_hospitals: nil, has_teaching_program: nil, is_major_academic_medical_center: nil, does_participate_in_survey: nil, status: "modified", net_revenue: nil, gross_revenue: nil, source_name: nil, source_id: nil, import_concat_key: nil, bigtime_id: nil, is_non_hc: nil, is_client: nil, contact_record_id: nil, form990_legacy_id: nil, number_of_beds: nil, tax_exempt_status: nil, delta: true>]

但是现在,如果我尝试按名称进行搜索,这绝对是我的模型的索引,我就不会得到我的记录。

(rdb:1) p Organization.search 'Allina', :retry_stale => true
[]

我试图修改选项无济于事,所以我想我只是尝试在星型模式中搜索单个字母。结果令人不安。

(rdb:1) p Organization.search 'C', :star => true, :retry_stale => true
[#<Organization id: 1, ein: nil, name: "Allina", created_at: "2012-09-11 15:03:04", updated_at: "2012-09-11 15:03:04", parent_id: nil, main_phone: "7867685187x894", fax: nil, email: nil, url: nil, crm_id: 1, target_market: false, notes: nil, form990_notes: nil, mec_revenue: 750000.0, legacy_id: nil, teaching_program_type_id: nil, sponsorship_type_id: nil, is_member_council_teaching_hospitals: nil, has_teaching_program: nil, is_major_academic_medical_center: nil, does_participate_in_survey: nil, status: "modified", net_revenue: nil, gross_revenue: nil, source_name: nil, source_id: nil, import_concat_key: nil, bigtime_id: nil, is_non_hc: nil, is_client: nil, contact_record_id: nil, form990_legacy_id: nil, number_of_beds: nil, tax_exempt_status: nil, delta: true>]

name字段中显然没有字母'C'。我的模型还将status字段编入索引,该字段设置为modified,并为state关联的address字段编制索引。该值为'CA',因此我认为可能 ThinkingSphinx正在找到该索引的记录。所以我想我会尝试搜索'CA',并猜测发生了什么:

(rdb:1) p Organization.search 'CA', :star => true, :retry_stale => true
[]

什么也没有!这种不稳定的行为是什么?好的,你们可能想看到一些配置和规范帮助器以及一些索引定义:

配置/ sphinx.yml

development:
  enable_star: 1
  min_infix_len: 1

test:
  enable_star: 1
  min_infix_len: 1

规格/ spec_helper.rb

Spork.prefork do
  require 'headless'

  headless = Headless.new(:display => 99)
  headless.start
  at_exit do
    headless.destroy
  end

  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'capybara/rspec'
  require 'database_cleaner'

  #require 'rspec/autorun'
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
  RSpec.configure do |config|
    config.use_transactional_fixtures = false
    config.treat_symbols_as_metadata_keys_with_true_values = true
    config.infer_base_class_for_anonymous_controllers = false

    config.include Devise::TestHelpers, :type => :controller
    config.extend ControllerMacros, :type => :controller
    config.include Warden::Test::Helpers, :type => :request
    config.include FactoryGirl::Syntax::Methods

    config.before(:suite) do
      DatabaseCleaner.strategy = :truncation, {
        :except => %w(sponsorship_types teaching_program_types title_groups positions tax_exempt_organization_types)}
    end

    config.before(:each) do
      DatabaseCleaner.start
    end

    config.after(:each) do
      DatabaseCleaner.clean
    end
  end
end

Spork.each_run do
end

的应用程序/模型/ organization.rb

  define_index do
    indexes :name, :sortable => true
    indexes address(:state), :as => :state
    indexes status

    set_property :delta => true
  end

1 个答案:

答案 0 :(得分:5)

我们在测试单元中编写了Thinking Spec测试。它工作得很好。 在spec / spec_helper.rb

中包含以下内容
require 'thinking_sphinx/test'
ThinkingSphinx::Test.init

此外,在每个测试中,在设置中使用ThinkingSphinx :: Test.start,在拆解时使用ThinkingSphinx :: Test.stop