On Tue, May 01, 2012 at 08:07:56PM +0200, Christian Johansen wrote:
> When testing for exceptions, I frequently want to test against the message.
> Not glue the exact message to the test, but "sweep" to make sure a few key
> words are part of the message. There's a problem with the signature that
> explains why the message is not there today:
>
> assert.exception(func[, type[, message]]);
>
> Where 'message' is a custom assertion message. It's impossible to tell if
> this should match the exception message with the current syntax. Do you
> have some concrete suggestion as to how we can fix this? I'd be happy to
> change the entire signature if we have a good enough solution.
I think modelling based on the QUnit signature would be a nice solution.
// Just match an exception
assert.exception(func);
// Match the exception message against a regular expression
assert.exception(func, /expected message/);
// Match the exception message exactly against a string
assert.exception(func, "expected message");
// Match the exception type. Note TypeError, not "TypeError"
assert.exception(func, TypeError);
// Call a custom callback to check the exception
assert.exception(func, function(err) {
return err.isValidException(); // Return true or false
});
Here's the QUnit implementation, it's pretty simple:
https://github.com/jquery/qunit/blob/master/qunit/qunit.js#L397
Shall I put it in a pull request? :)