Angularjs 如何为检查两个密码是否匹配的指令编写单元测试?
我正在用AngularJS 1.5编写一个应用程序,我想验证一个表单 表单有两个字段:密码和确认密码 我想要这些验证条件:缺少密码、缺少确认密码和密码必须匹配 我编写了一个自定义指令来处理密码匹配的情况 我想写一个单元测试来检查各种输入,然后再交给QA 我怎样才能为此编写测试Angularjs 如何为检查两个密码是否匹配的指令编写单元测试?,angularjs,unit-testing,karma-jasmine,Angularjs,Unit Testing,Karma Jasmine,我正在用AngularJS 1.5编写一个应用程序,我想验证一个表单 表单有两个字段:密码和确认密码 我想要这些验证条件:缺少密码、缺少确认密码和密码必须匹配 我编写了一个自定义指令来处理密码匹配的情况 我想写一个单元测试来检查各种输入,然后再交给QA 我怎样才能为此编写测试 /* confirmPassword.directive.js */ (function () { 'use strict'; angular .module('softworksSe
/* confirmPassword.directive.js */
(function () {
'use strict';
angular
.module('softworksSelfService')
.directive('confirmPwd', confirmPasswordInput);
function confirmPasswordInput() {
var directive = {
link: link,
scope: {
otherModelValue: '=confirmPwd'
},
require: 'ngModel'
};
return directive;
function link(scope, elements, attributes, ngModel) {
ngModel.$validators.match = function (modelValue) {
return modelValue == scope.otherModelValue;
};
scope.$watch('otherModelValue', function () {
ngModel.$validate();
});
}
}
})();
<input
id="confirmPassword"
type="password"
name="confirmPassword"
ng-model="data.confirmPassword"
placeholder="confirm password"
confirm-pwd="data.password"
required>
/*confirmPassword.directive.js*/
(功能(){
"严格使用",;
有棱角的
.module('softworksSelfService')
.指令(“confirmPwd”,confirmPasswordInput);
函数confirmPasswordInput(){
var指令={
链接:链接,
范围:{
otherModelValue:'=confirmPwd'
},
要求:'ngModel'
};
返回指令;
功能链接(范围、元素、属性、模型){
ngModel.$validators.match=函数(modelValue){
返回modelValue==scope.otherModelValue;
};
范围.$watch('otherModelValue',函数(){
ngModel.$validate();
});
}
}
})();