【发布时间】:2016-04-14 19:20:48
【问题描述】:
据我了解,$attr.$observe 会触发一次。所以有时在属性上使用 $watch。我希望对需要 ngModel
的指令进行单元测试scope.$watch(attr.ngModel, function (newValue) {
minlength = parseInt(attr.minLength);
scope.minLengthValidator(newValue);
});
由于这是在 Link 函数中使用范围,所以我似乎可以调用 $digest。
我的模拟属性是这样开始的......
html = angular.element("<input ng-model=\"myUnit\" min-length=\"3\">");
我不确定是否可以在我的规范中重新定义 element.attr('min-length') 并运行 $digest 或者是否有更复杂的方法,因为手表正在传递一个新值。
我的模拟设置的其余部分是这样的
$rootScope = $rootScope.$new();
element = $compile(html)($rootScope);
$rootScope.$digest(element);
controller = element.controller('ngModel');
scope = element.scope();
我之前没有在属性上测试过 $watch,所以任何指向我解决这个问题的方向都会非常感激。
【问题讨论】:
标签: javascript angularjs unit-testing