Я пытаюсь выполнить модульное тестирование директивы, которая использует ngModel и имеет трудности. Кажется, что функция ссылка моей директивы никогда не называют ...Углеродная директива единицы измерения, которая использует ngModel
Вот моя директива код:
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;
}
}
}
}
}]);
Вот мой тест спецификации:
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)');
});
});
});
Консоль вывода журнала «called» никогда не происходит и, очевидно, элемент в моей тестовой спецификации никогда не обновляется.
Что я делаю неправильно?
Я понял это ... Я забыл добавить свою директиву в массив файлов в моем файле karma.config. : S – mcottingham