Module: Minitest::Assertions

Defined in:
lib/minitest/assert_errors.rb

Overview

reopening to add additional functionality

Instance Method Summary collapse

Instance Method Details

#assert_have_error(expected_msg, klass = Minitest::Assertion, &blk) ⇒ Object Also known as: assert_error_raised, assert_returns_error

Assertion method to test for an error raised by Minitest

assert_have_error('error message') { assert(false, 'error message') }

proc { assert(false, 'error message') }.must_have_error('error message')

Produces an extensive error message, combining the given error message with the default error message, when something is wrong.

NOTE! The expected error message can be a String or Regexp.

assert_have_error(/error message.+Actual:\s+\"b\"/m) do
  assert_equal('a','b', 'error message')
end

# or

proc {
  assert_equal('a','b', 'error message')
}.must_have_error(/error message.+Actual:\s+\"b\"/m)


31
32
33
34
35
36
37
# File 'lib/minitest/assert_errors.rb', line 31

def assert_have_error(expected_msg, klass = Minitest::Assertion, &blk)
  e = assert_raises(klass) do
    yield(blk) if block_given?
  end
  assert_match(expected_msg, e.message) if expected_msg.is_a?(Regexp)
  assert_equal(expected_msg, e.message) if expected_msg.is_a?(String)
end

#assert_no_error(&blk) ⇒ Object Also known as: refute_error, refute_error_raised, assert_no_error_raised

Assertion method to test for no error being raised by Minitest

assert_no_error() { assert(true, 'error message') }

proc { assert(true) }.wont_have_error

Produces an extensive error message, combining the given error message with the default error message, when something is wrong.

NOTE! The expected error message can be a String or Regexp.

assert_no_error { assert_equal('a', :a, 'error message') }
  #=> "error message.\nExpected: \"a\"\n  Actual: :a"

proc {
  assert_equal('a', :a, 'error message')
}.wont_have_error
  #=> "error message.\nExpected: \"a\"\n  Actual: :a"


61
62
63
64
65
# File 'lib/minitest/assert_errors.rb', line 61

def assert_no_error(&blk)
  assert_silent do
    yield(blk) if block_given?
  end
end