Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined method `rollback' for nil:NilClass #669

Open
nicolasrouanne opened this issue Oct 20, 2020 · 1 comment
Open

Undefined method `rollback' for nil:NilClass #669

nicolasrouanne opened this issue Oct 20, 2020 · 1 comment

Comments

@nicolasrouanne
Copy link

nicolasrouanne commented Oct 20, 2020

I'm running around 2000 tests with rspec and ActiveRecord. Every now and then, let's say one out of 5 test suite runs (i.e. 1 out of 10,000 tests), a random test fails with the undefined method 'rollback' for nil:NilClass:

Failure/Error:
  DatabaseCleaner.cleaning do
    example.run
  end

NoMethodError:
  undefined method `rollback' for nil:NilClass

I'm using this configuration (taken from the readme)

DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
@danielpclark
Copy link

Try this:

require 'active_record'

module RefineRollback
  refine ::ActiveRecord::ConnectionAdapters::TransactionManager do
    def rollback_transaction(transaction = nil)
      @connection.lock.synchronize do
        transaction ||= @stack.pop
        transaction.try(:rollback) && transaction.rollback_records
      end 
    end 
  end 
end

RSpec.configure do |config|
  using RefineRollback
  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end 

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end 

  config.before(:each, js: true) do
    DatabaseCleaner.strategy = :truncation
  end 

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants