Ruby-on-rails – How to load db:seed data into test database automatically

ruby-on-railsseedtesting

I'm attempting to use the new standard way of loading seed data in Rails 2.3.4+, the db:seed rake task.

I'm loading constant data, which is required for my application to really function correctly.

What's the best way to get the db:seed task to run before the tests, so the data is pre-populated?

Best Answer

The db:seed rake task primarily just loads the db/seeds.rb script. Therefore just execute that file to load the data.

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

# or

Rails.application.load_seed

Where to place that depends on what testing framework you are using and whether you want it to be loaded before every test or just once at the beginning. You could put it in a setup call or in a test_helper.rb file.