Hello I'm quite new in testing. I tried a lot to test a function but can't succeed even after reading all Stackoverflow subjects on this. I hope you'll find a way to help me ..
Here is an extract of my html :
<form #myForm="ngForm">
Here is one of my comp:
@ViewChild('myForm')myForm: NgForm;
and I have a validate function :
public valider(): void {
if (this.myForm.form.valid) {
//doSomething
} else {
console.log('your form is not valid.');
}
}
Finally here is the test I'm trying to run:
it('should send an error when the form is not valid', () => {
fixture = TestBed.createComponent(myComponent);
comp = fixture.componentInstance;
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(comp.myForm.form.invalid).toBeTruthy();
comp.valider();
});
});
The error I get is:
Unhandled Promise rejection:', 'Cannot read property 'form' of undefined'
Thanks a lot guys