Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcannonfodder/82a7c7216506b4b20859 to your computer and use it in GitHub Desktop.
Save tcannonfodder/82a7c7216506b4b20859 to your computer and use it in GitHub Desktop.
Fix nested describes in controller tests in Rails 2.3
# fixes nested describes in controller tests
# this is a Rails bug apparently fixed in newer Rails versions
# https://github.com/rails/rails/issues/7743
# This was originally written by @madrobby for Freckle: https://letsfreckle.com
class ActionController::TestCase
def self.determine_default_controller_class(name)
name.split('::').reverse.map { |n|
safe_constantize(n.sub(/Test$/, ''))
}.compact.first
end
private
def self.safe_constantize(camel_cased_word)
begin
camel_cased_word.constantize
rescue NameError => e
raise unless e.message =~ /(uninitialized constant|wrong constant name) #{const_regexp(camel_cased_word)}$/ ||
e.name.to_s == camel_cased_word.to_s
rescue ArgumentError => e
raise unless e.message =~ /not missing constant #{const_regexp(camel_cased_word)}\!$/
end
end
def self.const_regexp(camel_cased_word) #:nodoc:
parts = camel_cased_word.split("::")
last = parts.pop
parts.reverse.inject(last) do |acc, part|
part.empty? ? acc : "#{part}(::#{acc})?"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment