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

Use Rails' transactional fixtures instead of database cleaner #114

Open
bmulholland opened this issue Jul 27, 2022 · 1 comment
Open

Use Rails' transactional fixtures instead of database cleaner #114

bmulholland opened this issue Jul 27, 2022 · 1 comment

Comments

@bmulholland
Copy link

bmulholland commented Jul 27, 2022

This builds on #26, I suppose

Rails has built-in transactional fixtures, which should mean database cleaner is never needed: https://anti-pattern.com/transactional-fixtures-in-rails

As far as I can tell, this just means including ActiveRecord::TestFixtures, then calling setup_fixtures and teardown_fixtures. See https://github.com/rails/rails/blob/812bae8167488ee5984043ef71125e055352b873/activerecord/lib/active_record/test_fixtures.rb

@bmulholland
Copy link
Author

I tried but ran into issues with threading. Specifically, I'd get errors like Read: #<ActiveRecord::ActiveRecordError: Cannot expire connection, it is owned by a different thread: #<Thread:0x0000000111657040@puma srv tp 004 and #<ActiveRecord::ConnectionTimeoutError: could not obtain a connection from the pool within 5.000 seconds (waited 5.003 seconds); all pooled connections were in use>

Here's what I tried writing in clean.rb. I refered to https://github.com/rails/rails/blob/812bae8167488ee5984043ef71125e055352b873/activerecord/lib/active_record/test_fixtures.rb and https://github.com/rspec/rspec-rails/blob/main/lib/rspec/rails/fixture_support.rb

class Fixtures
  include ActiveRecord::TestFixtures
  extend ActiveSupport::Concern

  def run_in_transaction?
    true
  end

  included do
    self.use_transactional_tests = true
    self.use_instantiated_fixtures = false
  end
end

@@fixtures ||= Fixtures.new
# teardown is done *before* each step to follow Cypress best practices of
# tearing down state only in *before* hooks:
# https://docs.cypress.io/guides/references/best-practices#Using-after-or-afterEach-hooks
if @@fixtures.respond_to?(:@fixture_connections, true) &&
   @@fixtures.send(:@fixture_connections).any?
  @@fixtures.teardown_fixtures
end
@@fixtures.setup_fixtures

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

1 participant