With RSpec, how to seed the database on load?
Asked Answered
U

6

58

I'm using rspec for testing w my rails 3 app. I need to seed the database before the tests start. How can I seed the database with the following:

/db/seeds.rb

["Admin", "Member"].each do |role_name|
  Role.find_or_create_by_name(role_name)
end

Thanks

Unwinking answered 14/5, 2011 at 18:43 Comment(0)
H
12

Try, something like this

rake db:seed RAILS_ENV=test

You can get a list of all rake commands doing

rake -T

If this is test data, you may want to look at putting it into fixtures which will be loaded on the start of the tests.

Hakim answered 14/5, 2011 at 19:10 Comment(1)
This will seed data for the development database, not the test database. Add RAILS_ENV=test to seed the test database.Joyous
H
215

In spec_helper.rb or rails_helper.rb:

RSpec.configure do |config|
  config.before(:suite) do
    Rails.application.load_seed # loading seeds
  end
end
Hards answered 12/11, 2013 at 13:38 Comment(6)
I needed to explicitly add the 'database_cleaner' gem to my :test group in my gemfile to get DatabaseCleaner working.Diplococcus
This worked great for me and now I'm wondering why it's not gotten more upvotes or shown up elsewhereSchnozzle
Not requiring another gem when using the basic seeding functionality is much nicer with this.Hynda
Works for me after I added to rails_helper.rb instead of spec_helper.rb.Roseline
How do i use this to add a different seeds file besides the main seeds file? I'd like a much smaller one for my testsPitchman
Hi @JeremyMoritz I would do this in similar way like here explained medium.com/@ethanryan/… . I hope this will help out.Hards
B
23

However Scott's solution surely works for you, I believe the better way to solve your problem was to put the code responsible for seeding your test database within RSpec's configure block:

I use SeedFu and in my spec_helper I have:

RSpec.configure do |config|

  # some other configuration code ...

  config.before(:suite) do
    # ...
    SeedFu.seed
    # ...
  end

  # some other configuration code ...

end
Bawdy answered 24/1, 2012 at 21:13 Comment(1)
Shouldn't this be in rails_helper rather than spec_helper?Adscription
H
12

Try, something like this

rake db:seed RAILS_ENV=test

You can get a list of all rake commands doing

rake -T

If this is test data, you may want to look at putting it into fixtures which will be loaded on the start of the tests.

Hakim answered 14/5, 2011 at 19:10 Comment(1)
This will seed data for the development database, not the test database. Add RAILS_ENV=test to seed the test database.Joyous
O
12

I've followed the raging debate over at Auto-load the seed data from db/seeds.rb with rake. Die-hards maintain that you should never load seed data for tests, but I take the more moderate stance that there are occasions where you might want to load seed data for specific tests, e.g. verifying that the seed data exists.

Unlike some answers given here, I do not recommend unconditionally loading the seeds from inside your spec_helper file. Instead, you can load your seeds using before :each or before :all inside just those test files that need the seeds, e.g.:

describe "db seed tests" do
  before(:each) do
    load "#{Rails.root}/db/seeds.rb" 
  end

  ...your test code here...
end

update

As @marzapower points out, if you go this route, your seeds.db file should clear each table before creating entries or use find_or_create_by methods. (Hint: the former is faster and more reliable.) This will prevent duplicate entries if you load the seeds.db file more than once.

Oller answered 4/8, 2012 at 23:40 Comment(2)
This approach only works for the first call after a db:reset. Every sequent call will duplicate data.Tilley
@marzapower: Good catch -- I should have mentioned that my seeds.db file always drops the tables before populating them for the very reason you mention. Alternately, the seeds.db file can use find_or_create to avoid creating duplicates.Oller
V
10

To load seeds in rspec you need to add it after database cleanup in confg.before(:suite) in spec_helper

config.before(:suite) do
  DatabaseCleaner.clean_with(:truncation)
  load Rails.root.join('db', 'seeds.rb')
end
Videogenic answered 7/11, 2013 at 7:22 Comment(1)
+1 to this way, it allows to load a different seed file for test (perhaps smaller ;)Streit
P
2

I ended up needing to use DatabaseCleaner to truncate the database, then load the rake task that does my seeding (because I use seedbank). After that, I wound up wrapping my tests in a transaction like on the database_cleaner README, so that each test could run with a freshly loaded site.

RSpec.configure do |config|
  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
    MyApplicationName::Application.load_tasks
    Rake::Task['db:seed'].invoke # loading seeds
  end
  config.around(:each) do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  end
end
Pazpaza answered 19/6, 2015 at 22:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.