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/)
In the negative case, you can test that an exception was not raised or that if an exception was raised that the type of exception was different (sounds confusing).
denies("test") { "foo" }.raises(Exception) # would pass
denies("test") { raise Exception }.raises(My::Exception) # would pass
denies("test") { raise Exception }.raises(Exception) # would fail
Instance Attribute Summary
Attributes inherited from AssertionMacro
Instance Method Summary collapse
-
#devaluate(actual_exception, expected_class, expected_message = nil) ⇒ Array
Supports negative/converse assertion testing.
-
#evaluate(actual_exception, expected_class, expected_message = nil) ⇒ Array
Supports positive assertion testing.
Methods inherited from AssertionMacro
#error, #expected_message, expects_exception!, #expects_exception?, #fail, #new_message, #pass, register, #should_have_message
Instance Method Details
#devaluate(actual_exception, expected_class, expected_message = nil) ⇒ Array
Supports negative/converse assertion testing. This is also where magic happens.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/riot/assertion_macros/raises.rb', line 41 def devaluate(actual_exception, expected_class, =nil) = actual_exception && actual_exception. if actual_exception.nil? pass .raises(expected_class) elsif expected_class != actual_exception.class if && !(.to_s =~ %r[#{}]) pass .raises(expected_class).() else pass .raises(expected_class) end else = .expected_to_not_raise(expected_class) if fail .().but.raised(actual_exception.class). (actual_exception.) else fail end end end |
#evaluate(actual_exception, expected_class, expected_message = nil) ⇒ Array
Supports positive assertion testing. This is where magic happens.
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/riot/assertion_macros/raises.rb', line 24 def evaluate(actual_exception, expected_class, =nil) = actual_exception && actual_exception. if actual_exception.nil? fail .expected_to_raise(expected_class).but.raised_nothing elsif expected_class != actual_exception.class fail .expected_to_raise(expected_class).not(actual_exception.class) elsif && !(.to_s =~ %r[#{}]) fail ()..not() else = .raises(expected_class) pass( ? .() : ) end end |