Kev*_*eim 6 javascript checkbox jasmine angularjs karma-runner
我的情况如下:
指示
scope: { foo:'=' },
template: '<input type="checkbox" ng-model="foo"/>'
Run Code Online (Sandbox Code Playgroud)
父控制器
$scope.foo = false;
Run Code Online (Sandbox Code Playgroud)
茉莉花试验
var cb = iElement.find('input');
$timeout(function() { // using $timeout to ensure $digest() isn't the issue.
cb.prop('checked',!cb.prop('checked'))
},0);
expect(cb.prop('checked')).toBe(true); // passes
expect($scope.foo).toBe(true); // doesn't pass
Run Code Online (Sandbox Code Playgroud)
我的问题:为什么$ scope.foo在我发出prop('checked')时不会更新,即使DOM确实如此(因为我在检查之后已经验证了).
这是一个jsbin大致演示了问题http://jsbin.com/kapifezi/2/edit
所以完整的答案是:你需要更改'checked'属性,然后触发click事件.
var input = element.find('input');
input.prop('checked',!input.prop('checked'));
// input.triggerHandler('click');
input.triggerHandler('change');
Run Code Online (Sandbox Code Playgroud)
谢谢凯文,这对我帮助很大!
click
经过进一步调查 -当您将 ng-model 添加到复选框之类的东西时,看起来 Angular 会添加一个侦听器。
因此,测试此问题的正确方法似乎是click event
在 jasmine 测试中对 DOM 对象发出 a 。