[rspec-users] initializer require weirdness only within rspec?

Patrick J. Collins patrick at collinatorstudios.com
Thu Feb 23 04:01:04 UTC 2012

So, I've got the following structure:

# config/application.rb
  config.autoload_paths += ["#{Rails.root}/lib"]

# initializers/core_ext.rb
  require "core_ext/array.rb"
  require "core_ext/hash.rb"
  require "core_ext/numeric.rb"
  require "core_ext/float.rb"
  require "core_ext/object.rb"

# lib/core_ext/*
  ( various opened classes with added methods )


So, I found that my specs were failing because these added methods did
not exist in the context of the tests..  yet in the console, and in the
app they do..  I added a "puts" call to the beginning of each require to
see what was happening.  In the console, I got all trues..  When running
rspec spec, I got trues for array, float, and numeric, but false for
object and hash!

....... Totally confused...  Simply changing the initializer to specify
Rails.root for those two files fixed the problem: 

# initializers/core_ext.rb
  require "core_ext/array.rb"
  require "#{Rails.root}/lib/core_ext/hash.rb"
  require "core_ext/numeric.rb"
  require "core_ext/float.rb"
  require "#{Rails.root}/lib/core_ext/object.rb"

This gives all trues with rspec spec, but--- why?

Patrick J. Collins

More information about the rspec-users mailing list