我需要将某些东西植入测试环境,特别是来自 CanCan 的用户角色。但它似乎加载了正在开发的所有内容。
lib/tasks / test_seed.rake
namespace :db do
namespace :test do
task :prepare => :environment do
Rake::Task["db:seed"].invoke
end
end
end
在运行 bundle exec rake db:test:prepare 后正确地拉入了 seed.rb
分贝/种子.rb
admin = Role.create( { name: "admin" }, :without_protection => true)
user = Role.create( { name: "user" }, :without_protection => true)
if Rails.env.production? || Rails.env.development?
admin = User.create!({ name: "Admin",
email: "admin@example.com",
password: "foobar",
password_confirmation: "foobar",
role_ids: 1 },
:without_protection => true)
admin.confirm!
if Rails.env.development?
48.times do |n|
name = Faker::Name.name
email = "example-#{n+1}@example.com"
password = "foobar"
fake = User.create!({ name: name,
email: email,
password: password,
password_confirmation: password },
:without_protection => true)
fake.confirm!
end
end
end
一切看起来都不错,但随后......
$ rails console test
Loading test environment (Rails 3.2.8)
> User.first
User Load (1.0ms) SELECT "users".* FROM "users" LIMIT 1
=> #<User id: 1, email: "admin@example.com" ..................
> Rails.env.development?
=> false
怎么了?