Module: SimpleAssertions::AssertRaises

Defined in:
lib/simple_assertions/assert_raises.rb

Overview

Adds attribute matcher to assert_raises.

Example

# old behaviour
assert_raises(StandardError) { ... }

# exact match on attribute message
assert_raises(RuntimeError, :message => "yay!") do
  raise "yay!"
end

# pattern match on attributes
assert_raises(MyError, :message => /foo/, :code => 23) do
  raise MyError, "foo bar", 23
end

Instance Method Summary collapse

Instance Method Details

#assert_raises(*args, &block) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/simple_assertions/assert_raises.rb', line 19

def assert_raises(*args, &block)
  attributes = args.last.is_a?(Hash) ? args.pop : {}
  super(*args, &block).tap do |exception|
    attributes.each do |attribute, expected|
      assert_respond_to exception, attribute
      actual = exception.public_send(attribute)
      msg = proc { "Expected #{mu_pp(expected)} to match #{mu_pp(actual)} for #{attribute} on #{mu_pp(exception)}" }
      matched = expected == actual || expected === actual
      assert matched, msg
    end
  end
end