![](/img/trans.png)
[英]How to write a unit test case for a custom validator for angular reactive forms?
[英]How to write unit test case for a custom validator for angular reactive forms
我是測試新手。 如何為以下方法編寫測試用例:
export class CustomErrorStateMatcher implements ErrorStatematcher {
isErrorState(control: FormControl,form:NgForm | FormGroupDirective | null){
return control && control.invalid && control.touched
}}
您可以創建控件的實例並在那里傳遞您的自定義驗證器。 然后為其分配一些值並測試預期的輸出。
下面是一個例子:
let control: FormControl;
describe('Phone validator: ', () => {
beforeAll(() => {
control = new FormControl('', [phoneValidator]);
});
it('should validate phone', () => {
control.setValue('not a phone number');
expect(control.valid).toBe(false); // invalid phone number
control.setValue('+49123456789');
expect(control.valid).toBe(true); // valid phone number
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.