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

问题描述

我正在使用 CucumberRSpecFactory Girl 来测试我的 Rails 应用程序.但我有几个主要包含 静态 数据的查找表.所以我试图找出在测试时填充这些的最佳方法.在 FactoryGirl 中单独执行它们似乎很乏味,我想远离固定装置.对于开发和生产,我将它们填充到我的 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, PopulatorFaker,你永远不会用完实验室老鼠!

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

然后可能在 before :each 块中

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

或者您可以在测试前仅使用 Populator 填充您的数据库.

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

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

07-03 07:18