如何编写密码和确认密码的测试用例密码匹配验证指令
How to write a test case for password and Confirm password PasswordMatching validation Directive
我正在为密码不匹配验证程序编写单元测试用例。如何为以下方法编写测试规范?
我尝试在互联网上搜索,但无法设置表单控件的值
export class PasswordMatchingValidation {
constructor() {}
static MatchPassword(AC: FormControl) {
const password = AC.get('password').value; // to get value in input tag
const confirmPassword = AC.get('confirmPassword').value; // to get value in
if (password !== confirmPassword) {
AC.get('confirmPassword').setErrors({
MatchPassword: true
});
} else {
return null;
}
}
/**
* validate if the passwords are matching
* @param ctrl Takes Form Control
*/
validate(ctrl: FormControl) {
return PasswordMatchingValidation.MatchPassword(ctrl);
}
}
嗯,首先你应该用一些虚拟数据初始化一个新的 FormControl
,然后用你之前作为参数的控件调用 PasswordMatchingValidation.MatchPassword
并断言结果。
const control = new FormControl({password: 'whatever'});
describe('PasswordMatchingValidation', () => {
it('should validate two identical passwords', () => {
const validation = PasswordMatchingValidation.MatchPassword(control);
expect(validation).toEqual({ MatchPassword: true });
});
});
让你的FormControl
像它应该的那样,当然也断言相反的情况
我正在为密码不匹配验证程序编写单元测试用例。如何为以下方法编写测试规范?
我尝试在互联网上搜索,但无法设置表单控件的值
export class PasswordMatchingValidation {
constructor() {}
static MatchPassword(AC: FormControl) {
const password = AC.get('password').value; // to get value in input tag
const confirmPassword = AC.get('confirmPassword').value; // to get value in
if (password !== confirmPassword) {
AC.get('confirmPassword').setErrors({
MatchPassword: true
});
} else {
return null;
}
}
/**
* validate if the passwords are matching
* @param ctrl Takes Form Control
*/
validate(ctrl: FormControl) {
return PasswordMatchingValidation.MatchPassword(ctrl);
}
}
嗯,首先你应该用一些虚拟数据初始化一个新的 FormControl
,然后用你之前作为参数的控件调用 PasswordMatchingValidation.MatchPassword
并断言结果。
const control = new FormControl({password: 'whatever'});
describe('PasswordMatchingValidation', () => {
it('should validate two identical passwords', () => {
const validation = PasswordMatchingValidation.MatchPassword(control);
expect(validation).toEqual({ MatchPassword: true });
});
});
让你的FormControl
像它应该的那样,当然也断言相反的情况