ruby - ThinkingSphinx 在 RSpec 中不起作用

标签 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,它索引了 addressstate 字段> 协会。该值是“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

app/models/organization.rb

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

    set_property :delta => true
  end

最佳答案

我们在测试单元中编写了 Thinking Spec 测试。它工作得很好。 在您的 spec/spec_helper.rb 中包含以下内容

require 'thinking_sphinx/test'
ThinkingSphinx::Test.init

此外,在每个测试中,在设置中有 ThinkingSphinx::Test.start,在拆卸中有 ThinkingSphinx::Test.stop

关于ruby - ThinkingSphinx 在 RSpec 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12372912/

相关文章:

ruby-on-rails - 从 Rails 中不同命名空间中的另一个 Controller 重定向/调用方法

ruby-on-rails - 同一表格 rails 模型中两个字段的唯一性

ruby-on-rails - Rails 和 ruby​​ 客户端之间的长轮询/推送事件

ruby-on-rails-3 - 巫术的身份验证错误 - 没有这样的方法创建

ruby - 如何 stub 方法调用,然后进行哈希查找?

Ruby 对象缓存

ruby-on-rails-3 - rspec 测试一个嵌套的 if 函数

ruby - rspec 错误 - 没有要加载的文件 - rspec/mocks (LoadError)

ruby-on-rails - 什么是好的 RSpec 自动化测试平台?

ruby-on-rails - 路由约束问题。为什么这不起作用,或者我如何在 Rails 3.1 中拥有多个几乎相同的约束?