Wrapping the function call in a Lambda, as recommended by David Norman, is certainly one good way to solve this problem.
You might add this to your testing utilities, though, if you're looking for a more readable solution. This function wraps your function in an object with a method withArgs
, which allows you to write the same statement in a more readable way. Ideally this would be built into Chai.
var calling = function(func) {
return {
withArgs: function(/* arg1, arg2, ... */) {
var args = Array.prototype.slice.call(arguments);
return function() {
func.apply(null, args);
};
}
};
};
Then use it like:
expect(calling(handleError).withArgs(true)).to.not.throw();
expect(calling(handleError).withArgs("anything else")).to.throw("stop js execution");
It reads like english!