Module: Test::Unit::Assertions

Defined in:
lib/assert_error.rb

Overview

:nodoc:#

Instance Method Summary collapse

Instance Method Details

#assert_error(err_type, *patterns, &block) ⇒ Object

A better ‘assert_raise’. patterns can be one or more Regexps, or a literal String that must match the entire error message.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/assert_error.rb', line 7

def assert_error(err_type,*patterns,&block)
  assert_not_nil block, "assert_error requires a block"
  assert((err_type and err_type.kind_of?(Class)), "First argument to assert_error has to be an error type")
  err = assert_raise(err_type) do
    block.call
  end
  patterns.each do |pattern|
    case pattern
    when Regexp
      assert_match(pattern, err.message) 
    else
      assert_equal pattern, err.message
    end
  end
end