lundi 23 mars 2015

Test a simple function in a directive, angular

This is my directive:



angular.module('clientApp')
.directive('positionDropDowns', function (CommonFactory) {
return {
templateUrl: 'template/position-drop-downs/position-drop-downs.html',
restrict: 'E',
scope: {
districtsWithSubObjects: '='
},
link: function postLink(scope, element, attrs) {

scope.hello = function(name){
return 'hello ' + name;
}
}
};
});


How do I test the hello function? I tried this:



describe('Directive: positionsDropDowns', function () {

// load the directive's module
beforeEach(module('clientApp'));

beforeEach(module('template/position-drop-downs/position-drop-downs.html'));

var element,
scope;

beforeEach(inject(function ($rootScope) {
scope = $rootScope.$new();

element = angular.element('<position-drop-downs></position-drop-downs>');

$rootScope.$digest();
}));

it('fn hello', inject(function ($compile) {
expect(element.scope.hello('john')).toBe("hello john");

}));
});


I get TypeError: undefined is not a function


Aucun commentaire:

Enregistrer un commentaire