UnexpectedError
When assertions fail in Unexpected they yield an
UnexpectedError
. This instance has several convenience methods for
retrieving information about the error.
UnexpectedError.getErrorMessage(options)
Returns the error message as a magicpen instance. The method uses the assertion error modes to produce the correct error message.
Notice that you must either provide an output or a format in the given
options. The format can be on of text
, ansi
or html
. As a
shortcut you can also just pass the output or format directly.
An alternative to calling getErrorMessage(output)
with an output,
you can append the error message to an output the following way:
This is useful if you want to combine multiple errors in one assertion:
When the assertion fails we get the following output:
UnexpectedError.parent
When assertions delegate to nested expect
calls the errors that are
thrown at each level are chained together through the parent
property. The error message is serialized lazily, so it is possible to
change the error hierarchy before the error is serialized, or extract
information from the hierarchy and use expect.fail to throw
a new error.
We could for example change the error mode for all the errors in the
chain to nested
:
UnexpectedError.getParents()
Return an array with all the ancestor errors to this error. See parent for more information.
UnexpectedError.getAllErrors()
Return an array with this error and all its ancestors. See parent for more information.
UnexpectedError.getDiff()
Find the first error in the hierarchy that contains a diff and return it. This method if useful if you want to hoist a diff from an ancestor error and combine it with a new message.
Notice that you must either provide an output or a format in the given
options. The format can be on of text
, ansi
or html
. As a
shortcut you can also just pass the output or format directly.
UnexpectedError.getDiffMethod()
In case you need to wrap diff with additional information you can
retrieve a method that will build a diff for you. It will find the
first ancestor error that contains a diff and return the method to
create the diff. Now you can delegate to that method from
expect.fail
:
UnexpectedError.getLabel()
Get a shortened representation of the error message that doesn't
repeat the subject, for example should equal 'bar'
rather than
expected 'foo' to equal 'bar'
. This makes it suitable for display next
to an existing rendering of the subject, such as inside a diff.