Method: Minitest::Assertions#assert_raises
- Defined in:
- lib/minitest/assertions.rb
#assert_raises(*exp) ⇒ Object
Fails unless the block raises one of exp
. Returns the exception matched so you can check the message, attributes, etc.
exp
takes an optional message on the end to help explain failures and defaults to StandardError if no exception class is passed. Eg:
assert_raises(CustomError) { method_with_custom_error }
With custom error message:
assert_raises(CustomError, 'This should have raised CustomError') { method_with_custom_error }
Using the returned object:
error = assert_raises(CustomError) do
raise CustomError, 'This is really bad'
end
assert_equal 'This is really bad', error.
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/minitest/assertions.rb', line 424 def assert_raises *exp flunk "assert_raises requires a block to capture errors." unless block_given? msg = "#{exp.pop}.\n" if String === exp.last exp << StandardError if exp.empty? begin yield rescue *exp => e pass # count assertion return e rescue Minitest::Assertion # incl Skip & UnexpectedError # don't count assertion raise rescue SignalException, SystemExit raise rescue Exception => e flunk proc { exception_details(e, "#{msg}#{mu_pp(exp)} exception expected, not") } end exp = exp.first if exp.size == 1 flunk "#{msg}#{mu_pp(exp)} expected but nothing was raised." end |