Created
August 24, 2014 21:52
-
-
Save tcannonfodder/82a7c7216506b4b20859 to your computer and use it in GitHub Desktop.
Fix nested describes in controller tests in Rails 2.3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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