使用jasmin测试角度复选框 [英] testing an angular checkbox using in jasmin
问题描述
我的情况如下:
指令
scope: { foo:'=' },
template: '<input type="checkbox" ng-model="foo"/>'
父控制器
$scope.foo = false;
茉莉花测试
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
我的问题:为什么$ scope.foo不会在我更新时更新发布道具('已检查'),即使DOM确实如此(正如我在检查后验证的那样)。
My question: why doesn't $scope.foo get updated when I issue the prop('checked') even though the DOM does (as I've verified after inspecting it).
这是一个jsbin,大致证明了问题 http://jsbin.com/kapifezi/2/edit
Here is a jsbin that roughly demonstrates the problem http://jsbin.com/kapifezi/2/edit
推荐答案
经过进一步调查 - 当你将ng-model添加到某个东西时,看起来像angular会添加一个点击
监听器像一个复选框。
After further investigation - it looks like angular will add a click
listener when you add an ng-model to something like a checkbox.
所以看起来正确的测试方法是发出点击事件
在茉莉花测试中的DOM对象上。
So it would seem that the correct method of testing this would be to issue a click event
on the DOM object from within the jasmine test.
这篇关于使用jasmin测试角度复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!