Why after_commit not running even with use_transactional_fixtures = false

后端 未结 5 2355
悲哀的现实
悲哀的现实 2021-02-19 03:03

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

RSpec.configure do |config|
  config.use_transactional_         


        
5条回答
  •  闹比i
    闹比i (楼主)
    2021-02-19 03:37

    This is similar to @jamesdevar's answer above, but I couldn't add a code block, so I have to make a separate entry.

    You don't have the change the strategy for the whole spec suite. You can keep using :transaction globally then just use :deletion or :truncation (they both work) as needed. Just add a flag to the relevant spec.

    config.use_transactional_fixtures = false
    
    config.before(:suite) do
      # The :transaction strategy prevents :after_commit hooks from running
      DatabaseCleaner.strategy = :transaction
      DatabaseCleaner.clean_with(:truncation)
    end
    
    config.before(:each, :with_after_commit => true) do
      DatabaseCleaner.strategy = :truncation
    end
    

    then, in your specs:

    describe "some test requiring after_commit hooks", :with_after_commit => true do
    

提交回复
热议问题