Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add assert.throwsException.
  • Loading branch information
cespare committed Mar 29, 2012
1 parent c7b3fa7 commit 5178719
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion shoulda.js
Expand Up @@ -61,6 +61,26 @@ var assert = {
this.fail("Expected " + this._printObject(expected) + " but received " + this._printObject(actual));
},

// It would be nice to call this 'throws', but all Java keywords are Javascript keywords and hence 'throws'
// is a JS keyword. Sigh.
throwsException: function(expression, expectedExceptionName) {
try {
expression();
} catch(exception) {
if (expectedExceptionName) {
if (exception.name === expectedExceptionName) return;
else {
assert.fail("Expected exception " + expectedExceptionName + " to be thrown but exception " +
exception.name + " was thrown instead.");
}
} else return;
}
if (expectedExceptionName)
assert.fail("Expected exception " + expectedExceptionName + " but no exception was thrown.");
else
assert.fail("Expected exception but none was thrown.");
},

fail: function(message) { throw new AssertionError(message); },

/* Used for printing the arguments passed to assertions. */
Expand Down Expand Up @@ -294,4 +314,4 @@ Stubs = {
stubProperties.object[stubProperties.propertyName] = stubProperties.original;
}
}
};
};

0 comments on commit 5178719

Please sign in to comment.