Skip to content

Commit

Permalink
Add support for options to the failure notifier.
Browse files Browse the repository at this point in the history
This is intended to support rspec/rspec-mocks#956.
  • Loading branch information
myronmarston committed May 29, 2015
1 parent 52500a6 commit 9d43fa0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
25 changes: 22 additions & 3 deletions lib/rspec/support.rb
Expand Up @@ -82,12 +82,31 @@ def self.failure_notifier=(callable)
thread_local_data[:failure_notifier] = callable
end

# @private
DEFAULT_FAILURE_NOTIFIER = lambda { |failure, _opts| raise failure }

def self.failure_notifier
thread_local_data[:failure_notifier] ||= method(:raise)
thread_local_data[:failure_notifier] || DEFAULT_FAILURE_NOTIFIER
end

def self.notify_failure(failure)
failure_notifier.call(failure)
def self.notify_failure(failure, options={})
arity = if failure_notifier.respond_to?(:arity)
failure_notifier.arity
else
failure_notifier.method(:call).arity
end

# TODO: remove these first two branches once the other repos have been
# updated to deal with the new two-arg interface.
if arity == 1
failure_notifier.call(failure)
elsif Method === failure_notifier && failure_notifier.name.to_sym == :raise
# `raise` accepts 2 arguments (exception class and message) but we
# don't want it to treat the opts hash as the message.
failure_notifier.call(failure)
else
failure_notifier.call(failure, options)
end
end

def self.with_failure_notifier(callable)
Expand Down
2 changes: 1 addition & 1 deletion spec/rspec/support_spec.rb
Expand Up @@ -123,7 +123,7 @@ def method_missing(name, *args, &block)
after { RSpec::Support.failure_notifier = @failure_notifier }
let(:error) { NotImplementedError.new("some message") }
let(:failures) { [] }
let(:append_to_failures_array_notifier) { failures.method(:<<) }
let(:append_to_failures_array_notifier) { lambda { |failure, _opts| failures << failure } }

def notify(failure)
RSpec::Support.notify_failure(failure)
Expand Down

0 comments on commit 9d43fa0

Please sign in to comment.