"AL" }, {:name => "Alask-6ren">
gpt4 book ai didi

ruby-on-rails - 如何使用 RSpec 在 Rails 测试环境中加载 seed.rb?

转载 作者:行者123 更新时间:2023-11-28 19:47:57 26 4
gpt4 key购买 nike

我有以下 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 时将数据仅播种到测试环境中一次?

最佳答案

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

rake RAILS_ENV=test db:seed

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

关于ruby-on-rails - 如何使用 RSpec 在 Rails 测试环境中加载 seed.rb?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4343435/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com