If you got a big project, chances are spec_helper is required through different ways, File.expand_path / File.join() / … which results in it being loaded several times!
Try it:
#spec_helper.rb print "YEP!"
rake spec -> “YEP!YEP!YEP!YEP!YEP!YEP!YEP!YEP!……”
So how to prevent it ?
Unify!
Unify it to ‘spec_helper’ for rspec or ‘./spec/spec_helper’ for minitest, which can be copied without modification (like adding ‘../../’) and prevents duplicate spec_helper calls
#unify_spec_helper.rb
files = Dir["spec/**/*_spec.rb"].reject{|file|File.directory?(file)}
files.each do |file|
lines = File.readlines(file)
lines = lines.map do |line|
if line =~ /require.*spec_helper/
"require 'spec_helper'\n"
else
line
end
end
File.open(file,'w'){|f| f.write lines.join('') }
end
Run: ruby unify_spec_helper.rb
One thought on “Stop spec_helper from being loaded multiple times”