How to Populate Lookup tables in Testing (Rails)

这一生的挚爱 提交于 2019-12-05 10:38:24

Use Factory Girl .sequence, Populator and Faker and you'll never run out of lab rats!

Factory.define(:model) do |m|
  m.sequence(:title)  { |n| "model-#{n}" }
  m.author            Faker::Name.name
  m.short             Populator.words(5)
  m.long              Populator.paragraphs(1..3)
end

Then maybe in a before :each block

@models = []
15.times { @models << Factory.create(:model) }

Or you can use only Populator to fill your database before tests.

Maybe something like

rake RAILS_ENV=test db:seed

in your test helper file?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!