I don't know why I get the error of "TypeError: Cannot read property 'form' of undefined
" I've tried every way to understand this error, but I can't
I tryng test new setup for spec.ts, but now return error "Cannot read property 'form' of undefined" :
spec.ts
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { async, ComponentFixture, fakeAsync, TestBed, waitForAsync } from '@angular/core/testing';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { CustomValidationService } from 'src/app/core/services/custom-validation/custom-validation.service';
import { UserService } from 'src/app/core/services/user/user.service';
import {FormsModule} from '@angular/forms';
import { AccountComponent } from './account.component';
describe('AccountComponent', () => {
let accountComponent: AccountComponent;
let fb:FormBuilder;
let customValidator: CustomValidationService;
let userService: UserService;
let fixture: ComponentFixture<AccountComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [AccountComponent],
imports: [FormBuilder, CustomValidationService, UserService],
providers:[FormBuilder]
}).compileComponents();
}))
it('',fakeAsync(()=>{
const testForm={
username:'test',
email: '[email protected]'
};
accountComponent.form.controls['username'].setValue(testForm.username)
}))
});
My component:
form: FormGroup;
formAlterPassword: FormGroup;
constructor(private fb: FormBuilder) {
this.form = this.fb.group({
username: ['', Validators.required],
email: ['', Validators.required]
});
this.formAlterPassword = this.fb.group({
currentPassword: ['', Validators.required],
password: ['', Validators.compose([Validators.required, Validators.minLength(12)])],
confirmPassword: ['', Validators.compose([Validators.required, Validators.minLength(12)])]
})
}```