单元测试自定义模板驱动的验证器

时间:2019-11-05 22:44:04

标签: angular unit-testing

我如何测试接收一个属性的自定义验证器?

@Directive({
    selector: '[customValidator][ngModel]',
    providers: [{ provide: NG_VALIDATORS, useExisting: CustomValidator, multi: true }]
})
export class CustomValidator implements Validator {

    @Input() min: number;

    constructor() {}

    validate(control: AbstractControl): ValidationErrors {
        return  control.value < this.min ? { error: true } : null;
    }
}

我尝试以编程方式创建CustomValidator类的实例,然后传递新的表单控件,但是当我尝试将min属性添加到FormControl时出现错误。

it('should set error on value less than min', () => {
        let customValidator = new CustonValidator();
        let input = new FormControl(1);
        input.min = 10; ///Error here
        ...
    });

0 个答案:

没有答案