我有以下创建input
元素的AngularJS指令。输入具有运行doIt()
函数的ng-change
属性。在我的指令的单元测试中,我想检查当用户更改输入时是否调用了doIt
函数。但是测试没有通过。尽管它在浏览器中手动测试时可以正常工作。
指令:
...
template: "<input ng-model='myModel' ng-change='doIt()' type='text'>"
测试:
el.find('input').trigger('change') // Dos not trigger ng-change
实时演示(ng-change):http://plnkr.co/edit/0yaUP6IQk7EIneRmphbW?p=preview
现在,如果我手动绑定change
事件而不是使用ng-change
属性,测试就会通过。
template: "<input ng-model='myModel' type='text'>",
link: function(scope, element, attrs) {
element.bind('change', function(event) {
scope.doIt();
});
}
实时演示(手动绑定):http://plnkr.co/edit/dizuRaTFn4Ay1t41jL1K?p=preview
有没有一种方法可以使用ng-change
并使其可测试?谢谢。
https://stackoverflow.com/questions/17626604
复制相似问题