Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

autoload_macros fails under Rails 3 (even in rails3 branch) #78

Closed
aanand opened this Issue · 8 comments

8 participants

@aanand

When executing script/rails console test, I get the following:

DEPRECATION WARNING: RAILS_ROOT is deprecated! Use Rails.root instead. (called from join at /Users/aanand/.bundle/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:40)
/Users/aanand/.bundle/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:40:in `join': can't convert # into String (TypeError)
    from /Users/aanand/.bundle/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:40:in `block in autoload_macros'
    from /Users/aanand/.bundle/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:40:in `map'
    from /Users/aanand/.bundle/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:40:in `autoload_macros'
    from /Users/aanand/.bundle/gems/shoulda-2.10.3/lib/shoulda/rails.rb:12:in `'
    from /Users/aanand/.bundle/gems/activesupport-3.0.0.beta/lib/active_support/dependencies.rb:167:in `require'
    from /Users/aanand/.bundle/gems/activesupport-3.0.0.beta/lib/active_support/dependencies.rb:167:in `block in require'
    from /Users/aanand/.bundle/gems/activesupport-3.0.0.beta/lib/active_support/dependencies.rb:537:in `new_constants_in'
    from /Users/aanand/.bundle/gems/activesupport-3.0.0.beta/lib/active_support/dependencies.rb:167:in `require'
    from /Users/aanand/.bundle/gems/shoulda-2.10.3/lib/shoulda/test_unit.rb:9:in `'
    from /Users/aanand/.bundle/gems/activesupport-3.0.0.beta/lib/active_support/dependencies.rb:167:in `require'
    from /Users/aanand/.bundle/gems/activesupport-3.0.0.beta/lib/active_support/dependencies.rb:167:in `block in require'
    from /Users/aanand/.bundle/gems/activesupport-3.0.0.beta/lib/active_support/dependencies.rb:537:in `new_constants_in'
    from /Users/aanand/.bundle/gems/activesupport-3.0.0.beta/lib/active_support/dependencies.rb:167:in `require'
    from /Users/aanand/.bundle/gems/shoulda-2.10.3/lib/shoulda.rb:8:in `'
    from /Users/aanand/.rvm/gems/ruby-1.9.1-p378/gems/bundler-0.9.4/lib/bundler/runtime.rb:28:in `require'
    from /Users/aanand/.rvm/gems/ruby-1.9.1-p378/gems/bundler-0.9.4/lib/bundler/runtime.rb:28:in `block (2 levels) in require'
    from /Users/aanand/.rvm/gems/ruby-1.9.1-p378/gems/bundler-0.9.4/lib/bundler/runtime.rb:27:in `each'
    from /Users/aanand/.rvm/gems/ruby-1.9.1-p378/gems/bundler-0.9.4/lib/bundler/runtime.rb:27:in `block in require'
    from /Users/aanand/.rvm/gems/ruby-1.9.1-p378/gems/bundler-0.9.4/lib/bundler/runtime.rb:26:in `each'
    from /Users/aanand/.rvm/gems/ruby-1.9.1-p378/gems/bundler-0.9.4/lib/bundler/runtime.rb:26:in `require'
    from /Users/aanand/.rvm/gems/ruby-1.9.1-p378/gems/bundler-0.9.4/lib/bundler.rb:71:in `require'
    from /Users/aanand/unspace/unspaced/config/application.rb:6:in `'
    from /Users/aanand/.bundle/gems/railties-3.0.0.beta/lib/rails/commands.rb:33:in `require'
    from /Users/aanand/.bundle/gems/railties-3.0.0.beta/lib/rails/commands.rb:33:in `'
    from script/rails:10:in `require'
    from script/rails:10:in `'

This happens even if I install Shoulda from the rails3 branch. It looks like the first argument passed to autoload_macros (i.e. root) is nil. Backing up a bit, I added the following to rails.rb:

puts "defined?(Rails.root) = #{defined?(Rails.root).inspect}"
puts "Rails.root           = #{Rails.root.inspect}"
puts "defined?(RAILS_ROOT) = #{defined?(RAILS_ROOT).inspect}"
puts "RAILS_ROOT           = #{RAILS_ROOT.inspect}"
puts "root                 = #{root.inspect}"

resulting in the following output:

defined?(Rails.root) = "method"
Rails.root           = nil
defined?(RAILS_ROOT) = "constant"
RAILS_ROOT           = nil
root                 = nil
DEPRECATION WARNING: RAILS_ROOT is deprecated! Use Rails.root instead. (called from join at /Users/aanand/.rvm/gems/ruby-1.9.1-p378/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:40)
/Users/aanand/.rvm/gems/ruby-1.9.1-p378/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:40:in `join': can't convert # into String (TypeError)

followed by the same stack trace.

So both Rails.root and RAILS_ROOT are nil at the time init.rb is called--I'd call that rather strange. I'm afraid my knowledge of Shoulda and Rails don't extend enough to say whether this problem is the fault of either.

@jstorimer

I was getting the same issue. Even tracing it back to shoulda.rb, the main library file that should be included by Rails, Rails.root is nil. Something funny going on here.

Oddly enough I installed shoulda as a rails plugin and everything worked. Not a good solution but works for the time being.

@aanand

Installing as a plugin works for me too. Odd indeed.

@mugwump

The reson for this is the

Bundler.require :default, Rails.env

in application.rb: This also requires shoulda, which in turn tries to autoload the macros - which fails, as Rails.root is not yet initialized, as the application is not yet loaded.

That is also consistent with the plugin working: plugins are only load after the application is initialized and has a proper Rails.root.

I am not sure about a good solution here: shoulda should not autoload macros on a require, I guess. I added a unless root==nil to autoload_macros.rb and load any needed macros in the testhelper.

@melito

The error described in the original post is a result of Rails.root returning a Pathname object instead of a String.

When calling autoload_macros in lib/shoulda/rails.rb just tack on a to_s to the root variable. That makes it work.

I'm on ruby 1.9.1-p378 / Rails 3

@hpoydar

Here's my patch: http://github.com/hpoydar/shoulda/commit/365b21f1a44d953a64beb8bbf134cd49f85256b7

Essentially, it works as a plugin. As a gem, you need to load up those macros manually.

@AlekSi

Workaround:

Gemfile:
gem "shoulda", :require => nil

test_helper.rb:
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', FILE)
require 'rails/test_help'
require 'shoulda'

@rmm5t
Owner

In Rails 3 and the latest stable version of shoulda, this should no longer be an issue. Is anyone still experiencing it?

Note, you may need to employ the following workaround for loading your own custom shoulda_macros by putting this at the bottom of your test_helper.rb:

Shoulda.autoload_macros Rails.root.to_s
@gabebw
Owner

I'm closing this.

@gabebw gabebw closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.