I need to test a directive, and it should throw an exception. How can I test that the exception was thrown, in jasmine?
The directives link function:
link: function() {
if(something) {
throw new TypeError('Error message');
}
}
I have not yet successfully implemented a test that actually catches the error and reports that the test was successful.
This is how I do it:
describe("myDirective", function() {
it("should throw an error", inject(function ($compile, $rootScope) {
function errorFunctionWrapper()
{
$compile(angular.element("<div my-directive></div>"))($rootScope);
}
expect(errorFunctionWrapper).toThrow();
}));
});
it("should throw an error", inject(function ($compile, $rootScope) {
expect(function () {
$compile(angular.element("<directive-name></directive-name>"))($rootScope.$new());
}).toThrow();
}));