我们有一个相当大的测试套件,暂时没有人使用过,在尝试重新测试时,我在运行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等。
显然,我对变量名称进行了模糊处理。
答案 0 :(得分:0)
尝试使用全局变量而不是常量。更好的是,尝试将初始化器放在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