Patrick J. Collins
2012-Feb-23 04:01 UTC
[rspec-users] initializer require weirdness only within rspec?
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 http://collinatorstudios.com
David Chelimsky
2012-Feb-23 04:37 UTC
[rspec-users] initializer require weirdness only within rspec?
On Feb 22, 2012, at 10:01 PM, "Patrick J. Collins" <patrick at collinatorstudios.com> wrote:> 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?Not certain and away from computer, but best guess is that another core_ext/object.rb lives in another lib (pretty sure active_support has this) that is, for whatever reason, showing up on the load path earlier when you''re running rspec. Also, autoload paths is for loading files using autoload e.g. the first time any code refs Thing, rails looks for a file at thing.rb within the autoload directories. The way you''re using it for lib is not only not as intended, but probably has no effect at all. Try commenting that line out and I suspect you''ll see no difference. As for how to fix, I tend to do this sort of thing by requiring "#{Rails.root}/lib/core_ext" from environment.rb, and have that file require the files in lib/core_ext/. HTH, David