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/)

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

#file, #line

Instance Method Summary collapse

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.

Parameters:

  • actual (Object)

    the value returned from evaling the Assertion block

  • expected_class (Class)

    the unexpected Exception class

  • expected_message (String, nil) (defaults to: nil)

    an optional exception message or message partial

Returns:



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, expected_message=nil)
  actual_message = actual_exception && actual_exception.message
  if actual_exception.nil?
    pass new_message.raises(expected_class)
  elsif expected_class != actual_exception.class
    if expected_message && !(actual_message.to_s =~ %r[#{expected_message}])
      pass new_message.raises(expected_class).with_message(expected_message)
    else
      pass new_message.raises(expected_class)
    end
  else
    message = new_message.expected_to_not_raise(expected_class)
    if expected_message
      fail message.with_message(expected_message).but.raised(actual_exception.class).
        with_message(actual_exception.message)
    else
      fail message
    end
  end
end

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

Supports positive assertion testing. This is where magic happens.

Parameters:

  • actual (Object)

    the value returned from evaling the Assertion block

  • expected_class (Class)

    the expected Exception class

  • expected_message (String, nil) (defaults to: nil)

    an optional exception message or message partial

Returns:



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, expected_message=nil)
  actual_message = actual_exception && actual_exception.message
  if actual_exception.nil?
    fail new_message.expected_to_raise(expected_class).but.raised_nothing
  elsif expected_class != actual_exception.class
    fail new_message.expected_to_raise(expected_class).not(actual_exception.class)
  elsif expected_message && !(actual_message.to_s =~ %r[#{expected_message}])
    fail expected_message(expected_message).for_message.not(actual_message)
  else
    message = new_message.raises(expected_class)
    pass(expected_message ? message.with_message(expected_message) : message)
  end
end