I have a Rails 3 application. I use after_save callbacks for some models and after_commit callbacks for one of the models. All of the code the code works fine, but during RSpec tests, the after_commit callback doesn't get called when I save a Thing
model.
e.g.
class ThingObserver < ActiveRecord:Observer
observe Thing
def after_commit(thing)
puts thing.inspect
end
end
If I change the method name to after_save
, it gets called fine during tests. I need to be able to use after_commit
for this particular model because in some situations the change to a "thing" happens in a web server, but the effect of the observer happens in a Sidekiq worker, and after_save
doesn't guarantee that the data was committed and available when the worker is ready for it.
The config for RSpec looks like this in spec/spec_helper.rb
Rspec.configure do |config|
#yada yada
config.use_transactional_fixtures = true
#yada yada
end
I have also adjusted rake db:create
so that it draws from a structure.sql file. In lib/tasks/db.rb
task setup: [ 'test:ensure_environment_is_test', 'db:create', 'db:structure:load', 'db:migrate', 'db:seed' ]
I did this so that I could run tests to ensure that the database was enforcing foreign key constraints.
Is there a way to run both the after_save and after_commit callbacks without making the Rspec use_transactional_fixtures == false?
Or, is there a way to set config.use_transactional_fixtures to 'false' just for that test, or for that test file?