mardi 22 septembre 2015

testing directive throw exception if attr is undefined

I have an angularjs directive that needs a resource attr to be defined in order to make some logic. The next is a minimal definition for it:

angular.module('myAppName')
    .directive('loadObjects', loadObjects);

function loadObjects($window) {
    var directive = {
        restrict: 'E',
        template: '<md-content class="md-whiteframe-z4" layout-padding></md-content>',
        link: function(scope, element, attrs) {
            if (angular.isUndefined(attrs.resource)) {
                throw 'resource attr is mandatory';
            }
        }
    };
    return directive;
}

then in my tests, I'm trying to test that an invalid template throw the exception.

describe('LoadObjects directive', function() {
    beforeEach(module('myAppName'));

    it('should throw an error if resource attr is not defined', inject(function($rootScope, $compile) {
        var scope = $rootScope.$new();
        var elem = angular.element('<load-objects></load-objects>');
        expect(function() { $compile(elem)(scope); }).toThrow('resource attr is mandatory');
    }));
});

But I'm getting the next err:

Expected function to throw 'resource attr is mandatory', but it threw TypeError: 'undefined' is not an object (evaluating '$window.Raven.captureMessage')

Also I've tried to use that directive in a template and I can see the exception there in the javascript console.

Any suggestion is welcome. Thanks for reading.

Aucun commentaire:

Enregistrer un commentaire