Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Seeds.rb开发环境也被应用到测试环境中_Ruby On Rails_Ruby_Ruby On Rails 3_Rspec_Tdd - Fatal编程技术网

Ruby on rails Seeds.rb开发环境也被应用到测试环境中

Ruby on rails Seeds.rb开发环境也被应用到测试环境中,ruby-on-rails,ruby,ruby-on-rails-3,rspec,tdd,Ruby On Rails,Ruby,Ruby On Rails 3,Rspec,Tdd,我需要将某些东西植入测试环境,特别是CanCan的用户角色。但它似乎承载了所有正在开发的东西 lib/tasks/test\uuseed.rake 在运行bundle exec rake db:test:prepare之后正确地拉入seeds.rb db/seeds.rb 一切看起来都很好,但是 $ rails console test Loading test environment (Rails 3.2.8) > User.first User Load (1.0ms) SELEC

我需要将某些东西植入测试环境,特别是CanCan的用户角色。但它似乎承载了所有正在开发的东西

lib/tasks/test\uuseed.rake

在运行bundle exec rake db:test:prepare之后正确地拉入seeds.rb

db/seeds.rb

一切看起来都很好,但是

$ 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

有什么问题吗?

在启动任务之前,您必须将环境变量ENV设置为test。

也许,您在开发和测试环境中使用的是同一个数据库。检查数据库。yml

尝试执行:


$RAILS_ENV=test rake db:seed

首先,尝试在终端中执行它: echo$RAILS_ENV

如果它是显示测试,那么你有你的答案。 如果不是,请验证config/database.yml,以查看它是否在两个环境中使用相同的数据库。
还要验证你的config/environment.rb

我不知道你的意思。名称空间以db:test:prepare为目标,应该在seeds.rb中进行控制。这可能是正确的答案,因为Rails默认为开发环境,然后在某个时候切换到测试环境,作为运行测试任务的一部分。在执行db:test:prepare之前,它不一定会将Rails.env切换到test。从Rails的角度来看,它所需要做的就是在启动测试套件之前切换环境。在我看来,您应该为dev/prod保留种子,并将测试设置留给测试套件本身。不,数据库正在正确使用。这是个问题,因为每次我运行测试时,开发数据库都会被清除,哈哈
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