Skip to content
Arctic Fox edited this page May 3, 2019 · 1 revision

Error matchers execute a block of code that could raise an error. The matcher could be used to expect that a specific error was raised, or that a "happy path" does not raise an error.

raise_error

raise_error

Checks that some block raises an error. The error's type and message can be inspected.

Syntax

raise_error
raise_error(TYPE)
raise_error(MESSAGE)
raise_error(TYPE, MESSAGE)

where TYPE and MESSAGE are the expected error type and message respectively. The message argument can be a string or regular expression.

The Crystal default Spec syntax is also available.

expect_raises { BLOCK }
expect_raises(TYPE) { BLOCK }
expect_raises(TYPE, MESSAGE) { BLOCK }

Examples

A block expectation should be used with this matcher.

expect { raise "oops" }.to raise_error # true

Checking the type and message of an error:

array = [1, 2, 3]
expect { array[5] }.to raise_error(IndexError) # true
expect { array[5] }.to raise_error(/bounds/i)  # true
Clone this wiki locally