vendredi 20 février 2015

Chai unit test of Angular directive: error not throwing

I have a directive that should throw an error when a falsy attribute value is passed. The relevant directive code in the compile function is this:



var tooltipKey = tAttrs.taTooltip;
if (!tooltipKey) throw new Error("tooltip key required");


This throws correctly in the browser when I pass an empty string, like so:



<div ta-tooltip=""></div>


I'm asserting this behavior in my unit test (Karma, Mocha, Chai), like so:



expect(function () {
var element = $compile('<div ta-tooltip=""></div>')(scope);
scope.$apply();
}).to.throw(new Error("tooltip key required");


I get this error message:


AssertionError: expected [Function] to throw 'Error: tooltip key required'


I don't know why the error isn't throwing. What am I missing?


Aucun commentaire:

Enregistrer un commentaire