Testing with Thinking Sphinx
Before you get caught up in the specifics of testing Thinking Sphinx using certain tools, it’s worth noting that no matter what the approach, you’ll need to turn off transactional fixtures and index your data after creating the appropriate records - otherwise you won’t get any search results.
Also: make sure you have your test environment using a different port
config/sphinx.yml (which you may need to create if you
haven’t already). If this isn’t done, then you won’t be able to run
Sphinx in your development environment and your tests at the same time
(as they’ll both want to use the same port for 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).
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
Don’t forget, you also need to turn transactional fixtures off. This
can be done on a global level in your
Or, you can tag either an entire feature or single scenarios with the
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
Before block, in one of your steps files (see below). Another
option is Ben Mabey’s Database
Cleaner library - and make
sure you use the truncation strategy.
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):
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:
You can turn off transactional features on a per-test basis within the test class definition:
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
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
stop for that
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:
If you need to manually process indexes, just use the
which defaults to all indexes unless you pass in specific names.