如何使用 RSpec 测试 ThinkingSphinx

发布于 2024-10-01 09:04:44 字数 418 浏览 2 评论 0原文

我在模型中有一个类方法,它调用 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 一起使用,请通过代码示例指导我

I have a class method in a model that calls thinking_sphinx's search() method. I need to check this class method.

I want to start, index or stop sphinx in my rspec test cases. I am trying with this piece of code.

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

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

and with this code in each test case before I fire the search query

ThinkingSphinx::Test.index

but still after I fire the search query, it gives me empty results though exact matches are there in the test db.

Please guide me with code examples if you are using rspec with thinking_sphinx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤君无依 2024-10-08 09:04:44

根据 David 的帖子,我们最终得到以下解决方案:

#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 策略。

Following David post, we end up with following solution:

#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

It switch specified tables to :truncation strategy, and after that switch them back to :trasaction strategy.

穿透光 2024-10-08 09:04:44

这是由于交易固定装置

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

您必须禁用您的交易装置。

在您的 rspec_helper.rb 中

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

全局禁用。

请参阅使用 RSpec 2 关闭某一规范的事务装置

This is due to transactional fixtures.

While ActiveRecord can run all its operations within a single transaction, Sphinx doesn’t have access to that, and so indexing will not include your transaction’s changes.

You have to disable your transactional fixtures.

In your rspec_helper.rb put

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

to disable globally.

See Turn off transactional fixtures for one spec with RSpec 2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文