Sha256: 3b70d08ecf050e2289ab9ed35b72b309119dd1582f116280c80c964956e1a879

Contents?: true

Size: 1.32 KB

Versions: 8

Compression:

Stored size: 1.32 KB

Contents

# This matcher will return:
#    1. TRUE if the code was run without exceptions
#    2. FALSE if the code was run but raised (only) the specified exception
#
# It *will* raise an exception if the block of code raises an exception other than
# (the exception specified)
#
# To use it
#
# expect {
#  code
# }.to never_raise(MySpecificException)
#
RSpec::Matchers.define :never_raise do |exception_class|
  global_result = nil

  def supports_block_expectations?
    true # or some logic
  end

  match do |block|
    block.call
  rescue exception_class => e
    global_result = "expected #{block.source_location[0]}:#{block.source_location[1]} to never raise #{exception_class.name}, but did: #{e.message}"
    false # we did NOT never raise this exception
  rescue RSpec::Expectations::ExpectationNotMetError => e
    global_result = "expectation failed inside block at #{block.source_location[0]}:#{block.source_location[1]}: #{e}"
    # give us a pretty error message in addition to the error message from the exception
    raise e
  rescue StandardError
    # handle other exceptions by reraising them. They are exceptional!!!
    # (also, no pretty error messages here)
    raise
  else
    true   # everything ran, nothing raised at all, thus code did in fact not raise anything
  end

  failure_message do |_player|
    global_result
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
4me-sdk-2.0.4 spec/support/matchers/never_raise.rb
4me-sdk-2.0.3 spec/support/matchers/never_raise.rb
4me-sdk-2.0.2 spec/support/matchers/never_raise.rb
4me-sdk-2.0.1 spec/support/matchers/never_raise.rb
4me-sdk-2.0.0 spec/support/matchers/never_raise.rb
4me-sdk-2.0.0.pre.rc.3 spec/support/matchers/never_raise.rb
4me-sdk-2.0.0.pre.rc.2 spec/support/matchers/never_raise.rb
4me-sdk-2.0.0.pre.rc.1 spec/support/matchers/never_raise.rb