Unit Testing with acts_as_ferret

Posted on 19 June 2008 by Johannes Fahrenkrug. Tags: Programming Ruby rails
I'm using acts_as_ferret for full text search in a Rails app I'm building. It works great and of course I want to unit test the full text search in my models. So I added an extremely simple test (I have two fixtures that have the matching string "bla" in on of their indexed fields):
def test_find_by_contents
  assert_equal 2, MyModel.find_by_contents('bla').total_hits
end
This is great, except for one small problem: it almost always fails! The total_hits are sometimes 1, sometimes 0 and hardly ever 2. Why? Because the data from the fixtures is rebuilt before every test case so ferret can't keep it's index up to date quickly enough. A very crude but simple solution is to rebuild the index before ferret-related tests:
def test_find_by_contents
  MyModel.rebuild_index
  assert_equal 2, MyModel.find_by_contents('bla').total_hits
end
Now it works as expected. This will affect performance, though. So if you have a huge test suite, you might want to consider using the preload fixtures plugin from ELC Technologies. I haven't tried this, but it most likely will also eliminate the need to rebuild the index before ferret related tests since the fixtures are not constantly dumped and rebuilt. If this was useful for you, please take a minute and recommend me:
Recommend Me
Thank you!

Comments

Gene Tani said...

see also

http://bugs.omdb.org/browser/trunk/test/unit/movie_ferret_index_test.rb

http://bugs.omdb.org/browser/trunk/test/unit/person_ferret_test.rb

September 07, 2008 07:17 PM

Comments

Please keep it clean, everybody. Comments with profanity will be deleted.

blog comments powered by Disqus