improved throw type detection and messaging

This commit is contained in:
Jake Luer 2011-12-18 06:40:44 -05:00
parent 0165fc068a
commit 89b107ca77

View file

@ -766,21 +766,25 @@ Assertion.prototype.keys = function(keys) {
Assertion.prototype.throw = function (constructor) {
new Assertion(this.obj).is.a('function');
constructor = constructor || Error;
var name = constructor.name
, thrown = false;
var thrown = false;
try {
this.obj();
} catch (err) {
thrown = true;
this.assert(
err instanceof constructor
, 'expected ' + this.inspect + ' to throw ' + name
, 'expected ' + this.inspect + ' to not throw ' + name);
return this;
if (constructor) {
this.assert(
err instanceof constructor && err.name == constructor.name
, 'expected ' + this.inspect + ' to throw ' + constructor.name + ' but a ' + err.name + ' was thrown'
, 'expected ' + this.inspect + ' to not throw ' + constructor.name );
return this;
} else {
thrown = true;
}
}
var name = (constructor ? constructor.name : 'an error');
this.assert(
thrown === true
, 'expected ' + this.inspect + ' to throw ' + name