如何使用rpec在Rails测试环境中加载seed.rb?

时间:2010-12-03 07:43:08

标签: ruby-on-rails testing activerecord

我有以下seeds.rb文件:

State.create  [ 
  {:name => "Alabama", :abbreviation => "AL" }, 
  {:name => "Alaska", :abbreviation => "AK" }, 
  {:name => "Arizona", :abbreviation => "AZ" }, 
  {:name => "Arkansas", :abbreviation => "AR" }, 
  {:name => "California", :abbreviation => "CA" }, 
  {:name => "Colorado", :abbreviation => "CO" }, 
  {:name => "Connecticut", :abbreviation => "CT" }, 
  {:name => "Delaware", :abbreviation => "DE" }, 
  {:name => "District Of Columbia", :abbreviation => "DC" }, 
  {:name => "Florida", :abbreviation => "FL" }, 
  {:name => "Georgia", :abbreviation => "GA" }, 
  {:name => "Hawaii", :abbreviation => "HI" }, 
  {:name => "Idaho", :abbreviation => "ID" }, 
  {:name => "Illinois", :abbreviation => "IL" }, 
  {:name => "Indiana", :abbreviation => "IN" }, 
  {:name => "Iowa", :abbreviation => "IA" }, 
  {:name => "Kansas", :abbreviation => "KS" }, 
  {:name => "Kentucky", :abbreviation => "KY" }, 
  {:name => "Louisiana", :abbreviation => "LA" }, 
  {:name => "Maine", :abbreviation => "ME" }, 
  {:name => "Maryland", :abbreviation => "MD" }, 
  {:name => "Massachusetts", :abbreviation => "MA" }, 
  {:name => "Michigan", :abbreviation => "MI" }, 
  {:name => "Minnesota", :abbreviation => "MN" }, 
  {:name => "Mississippi", :abbreviation => "MS" }, 
  {:name => "Missouri", :abbreviation => "MO" }, 
  {:name => "Montana", :abbreviation => "MT" }, 
  {:name => "Nebraska", :abbreviation => "NE" }, 
  {:name => "Nevada", :abbreviation => "NV" }, 
  {:name => "New Hampshire", :abbreviation => "NH" }, 
  {:name => "New Jersey", :abbreviation => "NJ" }, 
  {:name => "New Mexico", :abbreviation => "NM" }, 
  {:name => "New York", :abbreviation => "NY" }, 
  {:name => "North Carolina", :abbreviation => "NC" }, 
  {:name => "North Dakota", :abbreviation => "ND" }, 
  {:name => "Ohio", :abbreviation => "OH" }, 
  {:name => "Oklahoma", :abbreviation => "OK" }, 
  {:name => "Oregon", :abbreviation => "OR" }, 
  {:name => "Pennsylvania", :abbreviation => "PA" }, 
  {:name => "Rhode Island", :abbreviation => "RI" }, 
  {:name => "South Carolina", :abbreviation => "SC" }, 
  {:name => "South Dakota", :abbreviation => "SD" }, 
  {:name => "Tennessee", :abbreviation => "TN" }, 
  {:name => "Texas", :abbreviation => "TX" }, 
  {:name => "Utah", :abbreviation => "UT" }, 
  {:name => "Vermont", :abbreviation => "VT" }, 
  {:name => "Virginia", :abbreviation => "VA" }, 
  {:name => "Washington", :abbreviation => "WA" }, 
  {:name => "West Virginia", :abbreviation => "WV" }, 
  {:name => "Wisconsin", :abbreviation => "WI" }, 
  {:name => "Wyoming", :abbreviation => "WY"}
]

我能够使用rake db:seed命令将数据播种到开发环境中,但我对如何在测试环境中执行此操作感到困惑。我正在使用rspec和rake test:prepare命令,但这只是设置一个空数据库。

有没有办法在运行rake test:prepare时将数据只播种到测试环境中一次?

3 个答案:

答案 0 :(得分:12)

如果您只想在测试环境中运行rake db:seed,则可以运行此命令:

rake RAILS_ENV=test db:seed

事实上,您可以使用 RAILS_ENV 选项传递任何rails环境

答案 1 :(得分:7)

我能够添加......

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

到我的spec / spec_helper.rb文件,每次运行测试时都会运行我的种子文件。

答案 2 :(得分:1)

将其放入您的spec_helper中:

Rails.application.load_seed