In order for unit tests to really provide full coverage you need to make sure you are testing that your function throws exceptions when it’s supposed to (lack of arguments, bad arguments, etc).
Currently we’re rolled our own solution for this with the following function
function assertException(fMethod, aArg, sMessage)
{
var bCaught = false;
try
{
fMethod.apply(null, aArg);
}
catch (e)
{
bCaught = true;
}
if (!bCaught)
{
jsunit.fail(sMessage);
}
}
But would be nice if our jsunit natively provided this kind of functionality. In other unit testing packages I’ve seen things like assertFail or assertThrows.
Yes, I’ve checked that out…it’s actually what I use to make my work-around method above
jsunit.fail() is a way to force a unit test failure
what I’m looking for is a way to test for the successful throwing of an exception.
For instance:
function add(a, b) {
if (a == null | b == null) {
throw "ArgumentException";
}
return a + b;
}
function test_add(){
//Unit test should pass because "ArgumentException" was thrown
jsunit.assertException(add(null, null));
}