Showing posts tagged shoulda

Rails 3: Shoulda failing to load

If you’re trying to test a Rails 3 gem/plugin/engine and you’re using Bundler, you’re likely to run into an error like the following:

DEPRECATION WARNING: RAILS_ROOT is deprecated! Use Rails.root instead. 
/Library/Ruby/Gems/1.8/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:40:in `join': can't convert # into String (TypeError)
from /Library/Ruby/Gems/1.8/gems/shoulda-2.10.3/lib/shoulda/autoload_macros.rb:40:in `autoload_macros'

You can easily fix this by making the autoload of macros a no-op, and loading them later once the app is defined:

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'precious'

require 'test/unit'

# No-op the auto load
require 'shoulda/autoload_macros'
module Shoulda; class <<self; alias :original_autoload_macros :autoload_macros; def autoload_macros *a; end; end; end
require 'shoulda'

class Test::Unit::TestCase
  def app_factory &blk
    klass = Class.new Rails::Application
    klass.class_eval(&blk) if block_given?
    Shoulda.original_autoload_macros File.dirname(__FILE__)
    klass
  end 
end