Ensure block throws an error

assert_error  class (Exception)

Runs the block and ensures that it raises the correct Exception. Useful for asserting that an Exception will be raised. You may specify the particular Exception class, which defaults to Exception.

Introduced in v3.0

Examples

# Example 1

assert_error do
  play 70
end                        
                           



 
 
# Will throw an exception: "Assert error failed!" as the block
# contains no errors.



# Example 2

assert_error do
  1 / 0
end                        



 
 
# Will not throw an exception as the block contains an error.



# Example 3

assert_error ZeroDivisionError do
  1 / 0
end                        



 
 
# Will not throw an exception as the block contains a ZeroDivisionError.



# Example 4

assert_error ThreadError do
  1 / 0
end                        
                           



 
 
# Will throw an exception as the block contains a ZeroDivisionError rather than
# a ThreadError.