Sha256: 9b5f5ae8b259aa5cad7c8b746dd9cc83de44b8b67102f27cd0837bb80ac7e543

Contents?: true

Size: 1.37 KB

Versions: 10

Compression:

Stored size: 1.37 KB

Contents

module Riot
  # 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/)
  class RaisesMacro < AssertionMacro
    register :raises
    expects_exception!

    def evaluate(actual_exception, expected_class, expected_message=nil)
      actual_message = actual_exception && actual_exception.message
      if actual_exception.nil?
        fail should_have_message.raised(expected_class).but.raised_nothing
      elsif expected_class != actual_exception.class
        fail should_have_message.raised(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 # evaluate
  end # RaisesMacro
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
riot-0.11.4 lib/riot/assertion_macros/raises.rb
riot-0.11.4.pre lib/riot/assertion_macros/raises.rb
riot-0.11.3 lib/riot/assertion_macros/raises.rb
riot-0.11.2 lib/riot/assertion_macros/raises.rb
riot-0.11.1 lib/riot/assertion_macros/raises.rb
riot-0.11.0 lib/riot/assertion_macros/raises.rb
riot-0.10.13 lib/riot/assertion_macros/raises.rb
riot-0.10.13.pre lib/riot/assertion_macros/raises.rb
riot-0.10.12 lib/riot/assertion_macros/raises.rb
riot-0.10.12.pre lib/riot/assertion_macros/raises.rb