我正在try 对登录表单进行单元测试,但我无法按预期执行它.
试验台配置:
beforeEach(() => {
TestBed.configureTestingModule({
imports: [FormsModule],
declarations: [AuthComponent],
});
fixture = TestBed.createComponent(AuthComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('form should be invalid', () => {
component.authForm.controls['email'].markAsTouched();
expect(component.authForm.invalid).toBeTruthy();
});
当执行上述测试时,我收到以下错误
TypeError: Cannot read properties of undefined (reading 'markAsTouched')
.
使用fakeAsync
和whenstable
修改测试时,如下所示
it('form should be invalid', fakeAsync(() => {
fixture.whenStable().then(() => {
component.authForm.controls['email'].markAsTouched();
expect(component.authForm.invalid).toBeTruthy();
});
}));
测试结果显示为SPEC HAS NO EXPECTATIONS form should be invalid
.
不确定我做错了什么.