Skip to content

Instantly share code, notes, and snippets.

@cjheath
Created January 18, 2013 03:07
Show Gist options
  • Save cjheath/4562054 to your computer and use it in GitHub Desktop.
Save cjheath/4562054 to your computer and use it in GitHub Desktop.
A spec helper to help you find the origin of all exceptions occurring during your test run (including recovered ones), so you can reduce the abuse of exceptions for control flow. Some surprising facts emerge: a builtin == operator that raises a NoMethodError exception trying to call coerce() on nil... and other wonderful things.
if ENV["RSPEC_REPORT_EXCEPTIONS"]
class Exception
alias_method :old_initialize, :initialize
def self.exceptions_seen
@@seen ||= {}
end
def self.see args
stack = caller[1..-1]
# Don't show expected exceptions or RSpec internal ones
return if caller.detect{|s| s =~ %r{rspec/matchers/raise_error}} or
self.name =~ /^RSpec::/
stack.reject!{|s| s =~ %r{/rspec/}}
key = stack[0,4]+[self.name]
return if exceptions_seen[key]
exceptions_seen[key] = show = stack
args[0] = args[0].to_str if args[0].class.name == "NameError::message"
puts "#{self.name}#{args.inspect}:\n\t#{show*"\n\t"}"
end
def initialize *args, &b
self.class.see args
send(:old_initialize, *args, &b)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment