如何在测试(Rails)中填充查找表 [英] How to Populate Lookup tables in Testing (Rails)

查看:126
本文介绍了如何在测试(Rails)中填充查找表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Cucumber,Rspec和Factory Girl来测试我的Rails应用程序。但我有几个查找表,主要包含静态数据。所以我试图找出最好的方法来填充这些在测试时。在FactoryGirl单独做他们似乎乏味,我想远离Fixtures。对于开发和生产,我在我的seeds.rb文件中填充它们。

I am using Cucumber, Rspec, and Factory Girl for the testing of my Rails Application. But I have several lookup tables that contain mostly static data. So I'm trying to figure out the best way to populate these when testing. Doing them individually in FactoryGirl seems tedious and I'd like to stay away from Fixtures. For development and production, I populate them in my seeds.rb file.

谢谢!

推荐答案

使用Factory Girl .sequence, Populator Faker ,你永远不会用完实验室鼠!

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

然后也许在 code> block

Then maybe in a before :each block

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

或者,您可以在测试之前仅使用Populator填充数据库。

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

这篇关于如何在测试(Rails)中填充查找表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆