开发者

Clean way to add seeds data in Rails test environment

开发者 https://www.devze.com 2023-03-13 23:35 出处:网络
Rails 3 provides a seeds.rb to load minimal set of data for production environment. However I am unable to load this set of data properly in my test environment.

Rails 3 provides a seeds.rb to load minimal set of data for production environment. However I am unable to load this set of data properly in my test environment.

I've ended up applying this answer How to开发者_开发百科 load db:seed data into test database automatically?

setup :load_seeds

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

The problem is that this loads seed at every test, and this is very slow. An other problem is that this data disappears once the tests ran, so rails console does not work with my seed data.

I'm planning to migrating my seed data to fixtures later but I have not the time to do this now.

How can I only load this data once and then run my test using this seed data ?


Here's a very clean answer: How do I load seed.rb within a Rails test environment using rpec? You use rake RAILS_ENV=test db:seed


  • Create a 'test_seed.rb' file in your test folder
  • In 'test_helper.rb' require your test_seed file
  • Place your initialization (seeding) and any other db-wide actions in the test_seed.rb


Put this in your spec_helper or equivalent:

Rails.application.load_seed
0

精彩评论

暂无评论...
验证码 换一张
取 消