How to test ThinkingSphinx using RSpec

你离开我真会死。 提交于 2019-12-04 04:24:48

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.

David

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!