Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why after_commit not running even with use_transactional_fixtures = false

Transactional fixtures in rspec prevent after_commit from being called, but even when I disable them with

RSpec.configure do |config|
  config.use_transactional_fixtures = false
end

The after_commit callback does not run.

Here is a rails app with the latest rspec / rails that I have produced the issue on: git://github.com/sheabarton/after_commit_demo.git

like image 393
user545139 Avatar asked May 18 '12 17:05

user545139


2 Answers

One way around this is to trigger the commit callbacks manually. Example:

describe SomeModel do
  subject { ... }

  context 'after_commit' do
    after { subject.run_callbacks(:commit) }

    it 'does something' do
      subject.should_receive(:some_message)
    end
  end
end

A little late, but hope this helps others.

like image 143
JPowell Avatar answered Nov 03 '22 19:11

JPowell


In my case I resolved such problem with database_cleaner's settings placed below:

config.use_transactional_fixtures = false

config.before(:suite) do
  DatabaseCleaner.strategy = :deletion
  DatabaseCleaner.clean_with(:truncation)
end

config.before(:each) do
  DatabaseCleaner.start
end

config.after(:each) do
  DatabaseCleaner.clean
end

Thanks to Testing after_commit/after_transaction with Rspec

like image 9
Stepan Zakharov Avatar answered Nov 03 '22 21:11

Stepan Zakharov