Home > Software engineering >  TypeError: Cannot read properties of undefined (reading 'controls') at <Jasmine>
TypeError: Cannot read properties of undefined (reading 'controls') at <Jasmine>

Time:12-22

I run the test I get the error "TypeError: Cannot read properties of undefined (reading 'controls')".

How can I arrange to run controls in the test?

My function:

describe('Login Component Integrated Test', () => {
  let fixture: ComponentFixture<LoginComponent>;
  let loginSpy;
  function updateForm(userEmail, userPassword, organization) {
    fixture.componentInstance.loginForm.controls.username.setValue(userEmail);
    fixture.componentInstance.loginForm.controls.password.setValue(userPassword);
    fixture.componentInstance.loginForm.controls.organization.setValue(organization);
  }


CodePudding user response:

First of all, as we discussed in the comments, you have to actually assign a value to fixture. You just annotated the type of it. After that is done you have to add async keyword to the callback inside describe. Final working solution would look something like this:

describe('Login Component Integrated Test', async() => {
  let fixture: ComponentFixture<LoginComponent>;
  fixture = TestBed.createComponent(LoginComponent);
  let loginSpy;
  function updateForm(userEmail, userPassword, organization) {
    fixture.componentInstance.loginForm.controls.username.setValue(userEmail);
    fixture.componentInstance.loginForm.controls.password.setValue(userPassword);
    fixture.componentInstance.loginForm.controls.organization.setValue(organization);
  }
  • Related