1

For my Rails project, I use custom code to setup my database before each test. This is a lot of data, and it takes about 2 seconds every time. This data is the same for every test, so I want to avoid doing this setup on every test.

Rails has transactional fixtures, which rolls back the database to the state it was in before the test, but after the Rails fixtures. My issue is that it is also rolled back to the state it was in before my setup data was created.

Is there a way to prevent the transactional fixtures from rolling back my setup code? I assume I have to run my setup code sometime before the transaction is started, but where would this be?

Ralf
  • 14,655
  • 9
  • 48
  • 58

1 Answers1

1

Is it ok if this data is in the DB, identically, for all tests?

If so, you can follow the same advise from the following question, which was about getting the seed data to be used with the test fixtures: Prevent Rails test from deleting seed data

You want to add to the top of your test_helper.rb

require "#{Rails.root}/PATH/TO/TEST_DATA_GENERATOR.rb"
generate_test_data

After this, it seems like your test data will be generated once, and then handled transactionally like the fixtures. Hope this helps.

Community
  • 1
  • 1
ms-ati
  • 1,297
  • 1
  • 13
  • 17