【问题标题】:How to test ThinkingSphinx using RSpec如何使用 RSpec 测试 ThinkingSphinx
【发布时间】:2010-11-11 10:59:36
【问题描述】:

我在模型中有一个类方法,它调用thinking_sphinx 的search() 方法。我需要检查这个类方法。

我想在我的 rspec 测试用例中启动、索引或停止 sphinx。我正在尝试使用这段代码。

before(:all) do
  ThinkingSphinx::Test.start
end

after(:all) do
  ThinkingSphinx::Test.stop
end

在我触发搜索查询之前,在每个测试用例中使用此代码

ThinkingSphinx::Test.index

但是在我触发搜索查询之后,尽管测试数据库中有完全匹配,但它给了我空的结果。

如果您将 rspec 与 thinking_sphinx 一起使用,请用代码示例指导我

【问题讨论】:

  • 我们在一个需要索引 60 万篇文章的项目中有 TS。这是一个很大的失败。测试(正如您所发现的)是* ss 中的真正痛苦。我们正在迁移到使用 Solr 的 SunSpot。

标签: ruby-on-rails thinking-sphinx


【解决方案1】:

按照大卫的帖子,我们最终得到以下解决方案:

#spec/support/sphinx_environment.rb
require 'thinking_sphinx/test'

def sphinx_environment(*tables, &block)
  obj = self
  begin
    before(:all) do
      obj.use_transactional_fixtures = false
      DatabaseCleaner.strategy = :truncation, {:only => tables}
      ThinkingSphinx::Test.create_indexes_folder
      ThinkingSphinx::Test.start
    end

    before(:each) do
      DatabaseCleaner.start
    end

    after(:each) do
      DatabaseCleaner.clean
    end

    yield
  ensure
    after(:all) do
      ThinkingSphinx::Test.stop
      DatabaseCleaner.strategy = :transaction
      obj.use_transactional_fixtures = true
    end
  end
end

#Test
require 'spec_helper'
require 'support/sphinx_environment'

describe "Super Mega Test" do
  sphinx_environment :users do
    it "Should dance" do
      ThinkingSphinx::Test.index
      User.last.should be_happy
    end
  end
end

它将指定的表切换到 :truncation 策略,然后将它们切换回 :trasaction 策略。

【讨论】:

  • 如果您有任何意见,请随时发表。
  • @Max 你的代码看起来很有希望。代码“ThinkingSphinx::Test.init”应该放在哪里?另外Factory_Girl的数据创建代码应该放在哪里?我很难让他们工作。结果生成的空网页。我怀疑 TS 没有看到数据或 TS 没有启动/索引正确。
【解决方案2】:

这是由于事务性固定装置

虽然 ActiveRecord 可以在单个事务中运行其所有操作,但 Sphinx 无权访问它,因此索引不会包括您的事务的更改。

您必须禁用您的事务性装置。

在你的 rspec_helper.rb 中放

RSpec.configure do |config|
  config.use_transactional_fixtures = false
end

全局禁用。

Turn off transactional fixtures for one spec with RSpec 2

【讨论】:

    猜你喜欢
    • 2018-07-30
    • 2018-08-23
    • 2013-12-07
    • 2019-12-10
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    相关资源
    最近更新 更多