-
Notifications
You must be signed in to change notification settings - Fork 2
TestingASolandraObjectPoweredApplication
When testing an application that uses ActiveRecord, transactions are used to ensure that your database gets cleaned out between each test (ensuring a spiffy clean database for each run).
Since Cassandra and Sunspot don't support transactions, we need to take a different approach for cleaning up between tests. The following is what I use in my spec_helper.rb file:
config.before(:each) do SolandraObject::Base.recorded_classes = {} end config.after(:each) do Sunspot.remove_all! SolandraObject::Base.recorded_classes.keys.each do |klass| SolandraObject::Base.connection.truncate!(klass.column_family.to_sym) end end
Another thing to keep in mind when testing applications that use SO is that you will likely have to call Sunspot.commit a lot. The reason for this is that commits to the index are staged which means they can take some time for the records to appear. When you put it in and immediately ask for it back, it just might not be there. Calling Sunspot.commit causes you to wait for the commit to happen meaning that it will be there when you ask for it.