2

Rails 3 provides a seeds.rb to load minimal set of data for production environment. However I am unable to load this set of data properly in my test environment.

I've ended up applying this answer How to load db:seed data into test database automatically?

setup :load_seeds

def load_seeds
  load "#{Rails.root}/db/seeds.rb"
end

The problem is that this loads seed at every test, and this is very slow. An other problem is that this data disappears once the tests ran, so rails console does not work with my seed data.

I'm planning to migrating my seed data to fixtures later but I have not the time to do this now.

How can I only load this data once and then run my test using this seed data ?

Community
  • 1
  • 1
ybart
  • 876
  • 8
  • 23

3 Answers3

2

Here's a very clean answer: How do I load seed.rb within a Rails test environment using rpec? You use rake RAILS_ENV=test db:seed

srt32
  • 1,260
  • 1
  • 14
  • 27
0

Put this in your spec_helper or equivalent:

Rails.application.load_seed
thisismydesign
  • 21,553
  • 9
  • 123
  • 126
0
  • Create a 'test_seed.rb' file in your test folder
  • In 'test_helper.rb' require your test_seed file
  • Place your initialization (seeding) and any other db-wide actions in the test_seed.rb
tamersalama
  • 4,093
  • 1
  • 32
  • 35
  • In fact I already tried this approach (but instead requiring a specific test seed file, I used the seeds.rb provided by rails 3.0). Unfortunately, the seeds loads but gets immediately deleted when any test is done. I'll provide a gist about it later and will edit my question. – ybart Jun 21 '11 at 08:18