繁体   English   中英

spec_helper.rb中的“警告:已经初始化的常数”

[英]“warning: already initialized constant” in spec_helper.rb

我们有一个相当大的测试套件,有一段时间没有人使用过,并且尝试返回测试时,在运行bundle exec rspec spec/controllers/test_spec.rb时,我总是遇到这种情况:

spec/spec_helper.rb:82: warning: already initialized constant THIS_ID
spec/spec_helper.rb:83: warning: already initialized constant THAT_ID
spec/spec_helper.rb:84: warning: already initialized constant RANDOM_ID

并且没有测试完成。 我尝试的每个rspec测试文件上都出现相同的错误。 但是,当我禁用Spork时,一切运行良好(所有测试都失败了,但是很长一段时间都没有碰到它们)。

我的spec_helper.rb

Spork.each_run do
  # This code will be run each time you run your specs.
  FactoryGirl.reload
  Department.where(name: ['This', 'That', 'Random']).destroy_all
  FactoryGirl.create(:department, name: 'This')
  FactoryGirl.create(:department, name: 'That')
  FactoryGirl.create(:department, name: 'Random')
  THIS_ID = Department.where(name: 'This').first.id
  THAT_ID = Department.where(name: 'That').first.id
  RANDOM_ID = Department.where(name: 'Random').first.id
end

我们使用Spork,FactoryGirl等。

显然我混淆了变量名。

尝试使用全局变量而不是常量。 更好的是,尝试在before放入初始化程序

describe Something do
  before do
    @this_id = Department.where(name: 'This').first.id
    @that_id = Department.where(name: 'That').first.id
    @random_id = Department.where(name: 'Random').first.id
  end
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM