4

I use ActiveRecord Fixtures to define preset data for tests.

I use the database_cleaner gem to reset the database between tests. I follow the approach described by Avdi Grimm to use truncation for javascript tests, but transactions for non-javascript tests.

Most advice regarding database_cleaner tells you to set config.use_transactional_fixtures = false, without discussing the implications. The implication is that when AR creates the Fixture data, it does not start a transaction. The idea is that database_cleaner will handle starting the transaction, if you use the :transaction strategy.

The problem is that ActiveRecord creates the Fixture data before you get a chance to call DatabaseCleaner.start. That means the fixture data is not part of the transaction, so when database_cleaner rolls back the transaction, the fixture data remains, polluting follow up tests that may not expect that fixture data.

How do I get ActiveRecord to create the Fixture data within the context of the database_cleaner transaction?

I'm using Rails 4.1.0rc1, rspec-rails 2.14.2, rspec 2.14.1, database_cleaner 1.2.0.

Joshua Flanagan
  • 8,527
  • 2
  • 31
  • 40

0 Answers0