When has_error() fails, the error message is misleading, implying that an error was thrown, but did not match what was expected, yet Caught and Expected both match each other.
The following failure:
assert.has_error(function() end)
produces the following error message:
say/init.lua:46: bad argument #2 to 'format' (no value)
stack traceback:
say/init.lua:46: in function 's'
luassert/assert.lua:60: in function 'error'
Fix has_error() to show the proper error message of the form:
Expected a different error.
Caught:
...
Expected:
...
and update the error message to more accurately describe the error when no error message is passed in, showing:
Expected a different error.
Caught:
(no error)
Expected:
(error)
This way, it is a bit clearer that no error was thrown/caught, while an error was expected. If an expected error message is provided, that error message will be displayed instead of (error). If the function being
tested calls error(), with no arguments, the caught message will display (nil) instead of (no error), since nil is a valid error object.
When
has_error()
fails, the error message is misleading, implying that an error was thrown, but did not match what was expected, yetCaught
andExpected
both match each other.The following failure:
produces the following error message:
Fix
has_error()
to show the proper error message of the form:and update the error message to more accurately describe the error when no error message is passed in, showing:
This way, it is a bit clearer that no error was thrown/caught, while an error was expected. If an expected error message is provided, that error message will be displayed instead of
(error)
. If the function being tested callserror()
, with no arguments, the caught message will display(nil)
instead of(no error)
, sincenil
is a valid error object.