嗨,我正在用角写反应性形式的测试,我很难测试代码的这一部分。
setForm(): void {
const firstName = this.form.get('firstName');
const lastName = this.form.get('lastName');
lastName.disable();
lastName.clearValidators();
lastName.setValue('');
firstName.enable();
firstName.setValidators([Validators.required]);
}
这是FormService文件的一部分,我在其中设置了反应性表单控件、验证等。有方法测试吗?
发布于 2022-01-24 09:42:53
当您为公共API方法编写测试时,通常需要问自己:“在执行此方法之后,结果应该是什么?”在您的示例中,在执行setForm()
之后,应该启用firstName
控件并将其标记为必需的,并且应该禁用lastName
控件,并且不需要任何值。你的测试应该是这样的:
it('should enable the firstName control and disable the lastName control when setForm() is called', () => {
const firstName = component.form.get('firstName');
const lastName = component.form.get('lastName');
component.setForm();
expect(firstName.enabled).toBe(true);
expect(firstName.hasValidator(Validators.required)).toBe(true);
expect(lastName.disabled).toBe(true);
expect(lastName.value).toBeFalsy();
});
https://stackoverflow.com/questions/70830001
复制相似问题