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

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

问题描述

我有以下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"}
]

我能够将数据播种到开发环境中使用耙分贝:种子命令,但我很困惑,我怎么会在测试范围内做到这一点环境。我使用RSpec的和耙测试: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.

有没有办法运行,而只是一次播种数据到测试环境耙测试:prepare

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

推荐答案

如果你只是想运行耙分贝:在测试环境中种子,那么你就可以运行这个命令

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环境的 RAILS_ENV 选项

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

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