Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assertThrowsError should have a way to verify the generated error is the expected error #16

Open
mm-gmbd opened this issue Jun 4, 2019 · 0 comments

Comments

@mm-gmbd
Copy link
Contributor

mm-gmbd commented 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:

/**
   * 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
   */
  function assertThrowsError(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 failure
            return e;
        } else {
            throw message;
        }
      } else {
        return e;
      }
    }

    throw message;
  }


...


function foo(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) {
  return typeof e == "string" && e == "input 'a string' cannot be a string"
})

Now, the above assertThrowsError will actually throw, because the errorCheck function will return false.

@mm-gmbd 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant