Class: Riot::RaisesMacro
- Inherits:
-
AssertionMacro
- Object
- AssertionMacro
- Riot::RaisesMacro
- 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, =nil) = actual_exception && actual_exception. 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 && !(.to_s =~ %r[#{}]) fail("expected #{.inspect} for message, not #{.inspect}") else if pass("raises #{expected_class.inspect} with message #{.inspect}") else pass("raises #{expected_class.inspect}") end end end |