Angular unit testing form validation
expect(component.onSubmit).toHaveBeenCalledTimes(1)
Replace above line in below code, as you want to check onSubmit method not component.registerForm
it('should call onSubmit method', () => {
spyOn(component, 'onSubmit');
el = fixture.debugElement.query(By.css('button')).nativeElement;
el.click();
expect(component.registerForm).toHaveBeenCalledTimes(1);
});
Error: Expected validator to return Promise or Observable.
This means you added your multiple validators wrong. Instead of this:
this.registerForm = this.formBuilder.group({
name: ['', Validators.required],
email: ['', Validators.required, Validators.email],
phone: ['', Validators.required],
password: ['', Validators.required, Validators.minLength(6)],
confirmPassword: ['', Validators.required],
}
Try this:
this.registerForm = this.formBuilder.group({
name: ['', Validators.required],
email: ['', [Validators.required, Validators.email]],
phone: ['', Validators.required],
password: ['', [Validators.required, Validators.minLength(6)]],
confirmPassword: ['', Validators.required],
}
Notice how multiple validators are provided in an array, instead of just comma-seperated.
For your second error, you want to call
expect(component.onSubmit).toHaveBeenCalledTimes(1);
Instead of
expect(component.registerForm).toHaveBeenCalledTimes(1);