17

I'm using Factory Girl to populate my seed data and adding it to the db in seed.rb.

I'm then running my tests using Cucumber.

I have a price table that contains seed data that I want in all my environments.

I want rake db:seed to add it to my dev and test db's and for cucumber to be able to use that test seed. Then I'll add that seed data in prod also.

How can I ensure that my seed data is added to both dev and test db's?

rake db:seed #only adds it only to my dev database
Jamis Charles
  • 5,827
  • 8
  • 32
  • 42

3 Answers3

38

You can try something like this:

rake db:seed RAILS_ENV=test --trace
rake db:seed RAILS_ENV=production --trace
Christian
  • 1,872
  • 1
  • 14
  • 14
  • Will this change the environment only for that rake task? Do I need to reset it afterward? – Jamis Charles Jan 22 '11 at 14:53
  • Not really, you are just telling rake to use the parameters according to your database.yml config file. Anyway it is always a good idea to be verbose in your commands, at least when you are learning. You can run your rails app: scrip/server RAILS_ENV=development – Christian Jan 22 '11 at 14:59
9

Check out this answer from a similiar post.

I really think it's better to use factories to fill test database. And if you need seed data during your tests add it as a before :all block in spec_helper/test_helper.

Community
  • 1
  • 1
Mirko
  • 5,207
  • 2
  • 37
  • 33
  • This answer is better for multiple developers who'd pull your changes and run tests on their machines. Using this answer, you wouldn't have to tell them to read the docs, that would instruct them to run: 'rake db:seed RAILS_ENV=test --trace' every time. – Dominic May 13 '14 at 18:02
  • How is this related to the question? – Andrew Koster Dec 14 '21 at 07:18
1

You may want to visit this thread as well when working with a test database, especially since you do not want to persist data in your test database.

As your tests start to evolve you will most likely find yourself using the seed data, factories, as well as mocks, and with persistent data you will find yourself running into conflicts.

Personally I like to use a custom seed file for my test database, and load it in my rails_helper:

DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean

load "#{Rails.root}/db/custom_test_seed.rb"
DBrown
  • 5,111
  • 2
  • 23
  • 24