如何使用 RSpec 在 Rails 测试环境中加载 seed.rb? [英] How do I load seed.rb within a Rails test environment using RSpec?

查看:34
本文介绍了如何使用 RSpec 在 Rails 测试环境中加载 seed.rb?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下seeds.rb文件:

I have the following seeds.rb file:

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 命令,但这只是设置了一个空数据库.

I am able to seed the data into the development environment with a rake db:seed command, but I'm confused as to how I would do this within the test environment. I am using rspec and the rake test:prepare command, but this just sets up an empty database.

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

Is there a way to seed the data just once into the test environment while running rake test:prepare?

推荐答案

如果你只是想在测试环境中运行rake db:seed,那么你可以运行这个命令:

If you just want to run rake db:seed in test environment, then you can run this command:

rake RAILS_ENV=test db:seed

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

In fact, you can pass any rails environment with RAILS_ENV option

这篇关于如何使用 RSpec 在 Rails 测试环境中加载 seed.rb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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