Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to seed test data in Rails not using seeds.rb?

I have seeds.rb populating my development database. And I know I can easily apply seeds.rb to my test database using:

rake db:seed RAILS_ENV=test

However, I want a different seeds.rb file to populate my test database. Maybe seeds_test.rb. This must be an extremely common requirement among rails programmers, isn't it?

If there's no easy way to do this, like rake db:seed:seeds_test RAILS_ENV=test, then how would I create a rake task? I would think something like this in lib/tasks/test_seeds.rake:

namespace :db do
  desc "Seed data for tests"
  task :seed_test => :environment do
    load "#{Rails.root}/db/seeds_test.rb"
  end
end

I think that'll work, but then I would love for my rake task to automatically know to apply this only to the test database without me having to specify:

rake db:seed_test RAILS_ENV=test

How do I get the rake task so all I have to enter in the command line is:

rake db:seed_test

UPDATE: Another stackoverflow question was linked to where the answer was to do this:

Rails.env = 'test'

Unfortunately that doesn't actually do anything (at least as of Rails 4.2) :(.

like image 691
at. Avatar asked Sep 10 '25 19:09

at.


1 Answers

In db/seeds.rb, test for the value of Rails.env and execute the respective seeding:

#db/seeds.rb
case Rails.env
when 'development'
  # development-specific seeds ...
  # (anything you need to interactively play around with in the rails console)

when 'test'
  # test-specific seeds ...
  # (Consider having your tests set up the data they need
  # themselves instead of seeding it here!)

when 'production'
  # production seeds (if any) ...

end

# common seeds ...
# (data your application needs in all environments)

If you want to use separate files, just require them inside this structure, e.g.:

#db/seeds.rb
case Rails.env
when 'development'
  require 'seeds_development'    
when 'test'
  require 'seeds_test'
when 'production'
  require 'seeds_production'
end

require 'seeds_common'

or shorter

#db/seeds.rb
require "seeds_#{Rails.env}"
require 'seeds_common'
like image 63
das-g Avatar answered Sep 13 '25 11:09

das-g