Class: Riot::RaisesMacro

Inherits:
AssertionMacro show all
Defined in:
lib/riot/assertion_macros/raises.rb

Overview

Asserts that the test raises the expected Exception

asserts("test") { raise My::Exception }.raises(My::Exception)
should("test") { raise My::Exception }.raises(My::Exception)

You can also check to see if the provided message equals or matches your expectations. The message from the actual raised exception will be converted to a string before any comparison is executed.

asserts("test") { raise My::Exception, "Foo" }.raises(My::Exception, "Foo")
asserts("test") { raise My::Exception, "Foo Bar" }.raises(My::Exception, /Bar/)

Instance Method Summary collapse

Methods inherited from AssertionMacro

default, #error, expects_exception!, #expects_exception?, #fail, #pass, register

Instance Method Details

#evaluate(actual_exception, expected_class, expected_message = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/riot/assertion_macros/raises.rb', line 14

def evaluate(actual_exception, expected_class, expected_message=nil)
  actual_message = actual_exception && actual_exception.message
  if actual_exception.nil?
    fail("should have raised #{expected_class}, but raised nothing")
  elsif expected_class != actual_exception.class
    fail("should have raised #{expected_class}, not #{actual_exception.class}")
  elsif expected_message && !(actual_message.to_s =~ %r[#{expected_message}])
    fail("expected #{expected_message.inspect} for message, not #{actual_message.inspect}")
  else
    if expected_message
      pass("raises #{expected_class.inspect} with message #{expected_message.inspect}")
    else
      pass("raises #{expected_class.inspect}")
    end
  end
end