Skip to content

NoMethodError: undefined method 'state' for nil:NilClass #692

@lucasthomazoni

Description

@lucasthomazoni

Setup

- Ruby: 3.1.2
- Rails: 7.0.3.1
- PostgreSQL: 14.4
- OS: Ubuntu 20.04.4 LTS on WSL2
- GitHub Actions: 
  - Image: ubuntu-latest
    - PostgreSQL: 14.2

---

Gemfile.lock:
database_cleaner-active_record (2.0.1)
      activerecord (>= 5.a)
      database_cleaner-core (~> 2.0.0)
database_cleaner-core (2.0.1)

Error

From time to time, if a run my specs locally it might throw the error below, the fun thing is it errors for like two or three runs and then it passes (using same seed at all times);

This error also occurs occasionally on GitHub Actions, I have to re-run my workflow some times as well to make it pass (different seeds)

$ rspec spec/
 Failure/Error:
       DatabaseCleaner.cleaning do
         example.run
       end

     NoMethodError:
       undefined method `state' for nil:NilClass

                 transaction.rollback unless transaction.state.invalidated?
                                                        ^^^^^^
     # /home/lucas/.rvm/gems/ruby-3.1.2@perk-api-sch/gems/database_cleaner-active_record-2.0.1/lib/database_cleaner/active_record/transaction.rb:17:in `block in clean'
     # /home/lucas/.rvm/gems/ruby-3.1.2@perk-api-sch/gems/database_cleaner-active_record-2.0.1/lib/database_cleaner/active_record/transaction.rb:15:in `each'
     # /home/lucas/.rvm/gems/ruby-3.1.2@perk-api-sch/gems/database_cleaner-active_record-2.0.1/lib/database_cleaner/active_record/transaction.rb:15:in `clean'
     # /home/lucas/.rvm/gems/ruby-3.1.2@perk-api-sch/gems/database_cleaner-core-2.0.1/lib/database_cleaner/strategy.rb:32:in `cleaning'
     # /home/lucas/.rvm/gems/ruby-3.1.2@perk-api-sch/gems/database_cleaner-core-2.0.1/lib/database_cleaner/cleaners.rb:34:in `block (2 levels) in cleaning'
     # /home/lucas/.rvm/gems/ruby-3.1.2@perk-api-sch/gems/database_cleaner-core-2.0.1/lib/database_cleaner/cleaners.rb:35:in `cleaning'
     # ./spec/rails_helper.rb:77:in `block (2 levels) in <top (required)>'

Database Cleaner Config

DatabaseCleaner.strategy = :transaction
DatabaseCleaner[:active_record].clean_with :truncation, {
  except: %w(
    table_a
    table_b
    table_c
  )
}

config.around do |example|
  DatabaseCleaner.cleaning do
    example.run
  end
end

Any idea what might be causing this error and how to fix it?

Thanks for the gem and your attention.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions