How to load db:seed data into test database automatically?

Ruby on-RailsTestingSeed

Ruby on-Rails Problem Overview


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?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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.

Solution 2 - Ruby on-Rails

I'd say it should be

namespace :db do
  namespace :test do
    task :prepare => :environment do
      Rake::Task["db:seed"].invoke
    end
  end
end

Because db:test:load is not executed if you have config.active_record.schema_format = :sql (db:test:clone_structure is)

Solution 3 - Ruby on-Rails

Putting something like this in lib/tasks/test_seed.rake should invoke the seed task after db:test:load:

namespace :db do
  namespace :test do
    task :load => :environment do
      Rake::Task["db:seed"].invoke
    end
  end
end

Solution 4 - Ruby on-Rails

I believe Steve's comment above should be the correct answer. You can use Rails.application.load_seed to load seed data into your test envoironment. However, when and how often this data is loaded depends on a few things:

#Using Minitest There is no convenient way to run this file once before all tests (see this Github issue). You'll need to load the data once before each test, likely in the setup method of your test files:

# test/models/my_model_test.rb
class LevelTest < ActiveSupport::TestCase

  def setup
    Rails.application.load_seed
  end

  # tests here...

end

#Using RSpec

Use RSpec's before(:all) method to load seed data for all test for this model:

describe MyModel do
  before(:all) do
  Rails.application.load_seed
end

describe "my model..." do
  # your tests here
end

Hope this helps.

Solution 5 - Ruby on-Rails

Building on Matt's answer, if taking that sort of route, I recommend calling Rails.application.load_seed in a before(:suite) block in rspec_helper.rb rather than in a before(:all) block in any file. That way the seeding code is invoked only once for the entire test suite rather than once for each group of tests.

rspec_helper.rb:

RSpec.configure do |config|
  ...
  config.before(:suite) do
    Rails.application.load_seed
  end
  ...
end

Solution 6 - Ruby on-Rails

We're invoking db:seed as a part of db:test:prepare, with:

Rake::Task["db:seed"].invoke

That way, the seed data is loaded once for the entire test run, and not once per test class.

Solution 7 - Ruby on-Rails

Adding Rake::Task["db:seed"].invoke to the db:test:prepare rake task did not work for me. If I prepared the database with rake db:test:prepare, and then entered the console within the test environment, all my seeds were there. However, the seeds did not persist between my tests.

Adding load "#{Rails.root}/db/seeds.rb" to my setup method worked fine, though.

I would love to get these seeds to load automatically and persist, but I haven't found a way to do that yet!

Solution 8 - Ruby on-Rails

For those using seedbank, it changes how seeds are loaded, so you probably can't/don't want to use the load ... solution provided here.

And just putting Rake::Task['db:seed'].invoke into test_helper resulted in:

Don't know how to build task 'db:seed' (RuntimeError)

But when we added load_tasks before that, it worked:

MyApp::Application.load_tasks
Rake::Task['db:seed'].invoke

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLuke FranclView Question on Stackoverflow
Solution 1 - Ruby on-RailsryanbView Answer on Stackoverflow
Solution 2 - Ruby on-RailsEugene BolshakovView Answer on Stackoverflow
Solution 3 - Ruby on-RailsNick MView Answer on Stackoverflow
Solution 4 - Ruby on-RailsMattView Answer on Stackoverflow
Solution 5 - Ruby on-RailsMark SchneiderView Answer on Stackoverflow
Solution 6 - Ruby on-RailsjondahlView Answer on Stackoverflow
Solution 7 - Ruby on-RailsalexplsView Answer on Stackoverflow
Solution 8 - Ruby on-RailsGary S. WeaverView Answer on Stackoverflow