I am trying to use the unit test directive, which uses ngModel and has difficulties. It seems that the link function of my directive is never called ...
Here is my directive code:
coreModule.directive('coreUnit', ['$timeout', function ($timeout) { return { restrict: 'E', require: '?ngModel', template: "{{output}}", link: function (scope, elem, attrs, ngModelCtrl) { ngModelCtrl.$render = function () { render(ngModelCtrl.$modelValue); }; console.log("called"); function render(unit) { if (unit) { var output = '(' + unit.numerator + (unit.denominator == '' ? '' : '/') + unit.denominator + (unit.rate == 'NONE' || unit.rate == '' ? '' : '/' + unit.rate) + ')'; scope.output = output == '()' ? '' : output; } } } } }]);
Here is my test spec:
describe('core', function () { describe('coreUnitDirective', function () { beforeEach(module('core')); var scope, elem; var tpl = '<core-unit ng-model="myUnit"></core-unit>'; beforeEach(inject(function ($rootScope, $compile) { scope = $rootScope.$new(); scope.myUnit = {}; elem = $compile(tpl)(scope); scope.$digest(); })); it('the unit should be empty', function () { expect(elem.html()).toBe(''); }); it('should show (boe)', function () { scope.myUnit = { numerator: 'boe', denominator: "", rate: "" }; scope.$digest(); expect(elem.html()).toContain('(boe)'); }); }); });
The console log output is called never, and obviously the item in my test spec is never updated.
What am I doing wrong?
source share