You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Right now assertThrowsError simply checks to make sure that whatever is passed throws an error, but it should have a way to verify that the error thrown is the expected error (rather than something else).
This comes from an oversight in some recently-written test code. Tests were written to ensure that errors were being thrown in certain conditions, but there was actually a bug in the error-throwing itself. It was unclear that there was a problem because, hey, an error was being thrown "as expected."
Now, considering the error string itself may be hard to exactly represent (as it contains the offending line number), maybe the most flexible solution would be to allow an errorChecker anonymous callback that is run on the error:
/** * Assert that function throws an erorr * @param {function} fn * @param {table|userdata|class|instance|meta} ctx * @param {array} args - arguments for the function * @param {string} message * @param {function} errorCheck - a user-defined function that checks the thrown error contents to ensure they are expected * @return {error} error thrown by function*/functionassertThrowsError(fn, ctx, args = [], message ="Function was expected to throw an error", errorCheck =null) {
this.assertions++;
args.insert(0, ctx)
try {
fn.pacall(args);
} catch (e) {
if (errorCheck) {
if (errorCheck(e) ==true) { //errorCheck must return `true` to indicate that the error was as expected, otherwise it is interpreted as failurereturn e;
} else {
throw message;
}
} else {
return e;
}
}
throw message;
}
...functionfoo(bar) {
if (typeof bar =="string") {
throw"input '"+ barrr +"' cannot be a string"//note that there is a misspelling in the variable name, which will cause this to throw unexpectedly
}
...
}
this.assertThrowsError(foo, this, ["a string"], "Should throw an error because the input cannot be a string", function(e) {
returntypeof e =="string"&& e =="input 'a string' cannot be a string"
})
Now, the above assertThrowsError will actually throw, because the errorCheck function will return false.
The text was updated successfully, but these errors were encountered:
mm-gmbd
changed the title
assertThrowsError should have a way to verify the generated is the expected error
assertThrowsError should have a way to verify the generated error is the expected error
Jun 4, 2019
Right now
assertThrowsError
simply checks to make sure that whatever is passed throws an error, but it should have a way to verify that the error thrown is the expected error (rather than something else).This comes from an oversight in some recently-written test code. Tests were written to ensure that errors were being thrown in certain conditions, but there was actually a bug in the error-throwing itself. It was unclear that there was a problem because, hey, an error was being thrown "as expected."
Now, considering the error string itself may be hard to exactly represent (as it contains the offending line number), maybe the most flexible solution would be to allow an
errorChecker
anonymous callback that is run on the error:Now, the above
assertThrowsError
will actually throw, because theerrorCheck
function will return false.The text was updated successfully, but these errors were encountered: