Class: Riot::RaisesKindOfMacro
- Inherits:
-
AssertionMacro
- Object
- AssertionMacro
- Riot::RaisesKindOfMacro
- Defined in:
- lib/riot/assertion_macros/raises_kind_of.rb
Overview
Asserts that the test raises the expected exception, or one of its subclasses. Thus, the following assertions pass:
asserts("test") { raise My::Exception }.raises(My::Exception)
should("test") { raise My::Exception }.raises(My::Exception.superclass)
The following, however, fails:
asserts("test") { raise My::Exception.superclass }.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/)
You can use the negative form to assert that no exception is raised at all:
denies("test") {
# do stuff
}.raises_kind_of Exception
It can be used to check that a particular class of exception is not raised, in which case you should be aware that raising another kind of exception will not produce a failure.
denies("test") { raises ArgumentError }.raises_kind_of ArgumentError # fails
denies("test") { raises Class.new(ArgumentError) }.raises_kind_of ArgumentError # fails
denies("test") { raises "this doesn't work" }.raises_kind_of ArgumentError # passes
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.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/riot/assertion_macros/raises_kind_of.rb', line 51 def devaluate(actual_exception, expected_class, =nil) = actual_exception && actual_exception. if !actual_exception pass .raises_kind_of(expected_class) elsif !actual_exception.is_a?(expected_class) if && !(.to_s =~ %r[#{}]) pass .raises_kind_of(expected_class). () else pass .raises_kind_of(expected_class) end else = .expected_to_not_raise_kind_of(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.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/riot/assertion_macros/raises_kind_of.rb', line 33 def evaluate(actual_exception, expected_class, =nil) = actual_exception && actual_exception. if !actual_exception fail .expected_to_raise_kind_of(expected_class).but.raised_nothing elsif !actual_exception.is_a?(expected_class) fail .expected_to_raise_kind_of(expected_class).not(actual_exception.class) elsif && !(.to_s =~ %r[#{}]) fail ()..not() else = .raises_kind_of(expected_class) pass( ? .() : ) end end |