Testing with Thinking Sphinx

Unit Tests and Specs

It’s recommended you stub out any search calls, as Thinking Sphinx should ideally only be used in integration testing (whether that be via Cucumber or other methods).

Cucumber

As of version 1.3.2, Thinking Sphinx has a helper object to make combining Thinking Sphinx and Cucumber quite easy. You’ll need to add the following two lines to your features/support/env.rb file:

require 'cucumber/thinking_sphinx/external_world'
Cucumber::ThinkingSphinx::ExternalWorld.new

And you also need to turn transactional fixtures off:

Cucumber::Rails::World.use_transactional_fixtures = false

The reason for this is that 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.

The added complication to this is that you’ll probably want to clear all the data from your database between scenarios. This can be done within the Before block, in one of your steps files (see below). Another option is Ben Mabey’s Database Cleaner library.

Before do
  # Add your own models here instead.
  [Article, User].each do |model|
    model.delete_all
  end
end

Once this is all set up, then Sphinx will automatically index and start the daemon when you run your features – but only once at the very beginning, not for every scenario (as that could be quite slow).

To re-index during specific scenarios, I recommend adding steps something like the following (to be called after preparing your model data, but before Webrat browses the application):

Given 'the Sphinx indexes are updated' do
  # Update all indexes
  ThinkingSphinx::Test.index
  sleep(0.25) # Wait for Sphinx to catch up
end

Given 'the Sphinx indexes for articles are updated' do
  # Update specific indexes
  ThinkingSphinx::Test.index 'article_core', 'article_delta'
  sleep(0.25) # Wait for Sphinx to catch up
end

Delta indexes (if you’re using the default approach) will automatically update just like they do in a normal application environment.

Any suggestions to improve this workflow are very much welcome.

Rails Functional and Integration Tests

In much the same way, Thinking Sphinx can be used in traditional functional and integration tests. You’ll want to add the following lines to your test_helper.rb file:

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

To actually have Sphinx running, you have a few options…

If you want it running constantly for all of your tests, you can call start_with_autostop in your test_helper.rb file:

ThinkingSphinx::Test.start_with_autostop

However, you probably don’t want Sphinx running for your unit tests, and so it’s recommended you just start and stop Sphinx as required. ThinkingSphinx::Test has methods named start and stop for that very purpose:

test "Searching for Articles" do
  ThinkingSphinx::Test.start
  
  get :index
  assert [@article], assigns[:articles]
  
  ThinkingSphinx::Test.stop
end

You can also wrap the code that needs Sphinx in a block called by ThinkingSphinx::Test.run, which will start up and stop Sphinx either side of the block:

test "Searching for Articles" do
  ThinkingSphinx::Test.run do
    get :index
    assert [@article], assigns[:articles]
  end
end

If you need to manually process indexes, just use the index method, which defaults to all indexes unless you pass in specific names.

ThinkingSphinx::Test.index # all indexes
ThinkingSphinx::Test.index 'article_core', 'article_delta'