title: Angular Unit Test should create FAILED TypeError: Cannot read properties of undefined (reading 'preferences')
body: <p>I am trying to create cover all the line (Jasmine / Karma) but I am getting the error as <strong>Cannot read property 'preferences' of undefined</strong></p>
  <p>Here is my code for component code.</p>
  <pre><code>    export class PreferComponent implements OnInit {
    @Output() updatePreferences = new EventEmitter&lt;any&gt;();
    @ViewChild('settingModal', { static: true }) settingModal: any;
    private data: any;
    preferences = [];
    maxCols: number;
    selectedPref: number;
    constructor() { }
  
    ngOnInit(): void {
      this.preferences = JSON.parse(JSON.stringify(this.data.preferences));
      this.maxCols = this.data.maxCols;
      this.selectedPref = this.data.selectedPref;
      this.checkPreferences();
    }
  
    setPreferences(col, val) {
      if (val) {
        col.isPreferredColumn = true;
        this.selectedPref++;
      } else {
        col.isPreferredColumn = false;
        this.selectedPref--;
      }
      this.checkPreferences();
    }
  }
  </code></pre>
  <p>Here is the below code for spec which I tried:</p>
  <pre><code>   fdescribe('PreferComponent ', () =&gt; {
    let component: PreferComponent ;
    let fixture: ComponentFixture&lt;PreferComponent &gt;;
  
    beforeEach(async () =&gt; {
      await TestBed.configureTestingModule({
        declarations: [PreferencesComponent],
        schemas: [CUSTOM_ELEMENTS_SCHEMA]
  
      })
        .compileComponents();
    });
  
    beforeEach(() =&gt; {
      fixture = TestBed.createComponent(PreferComponent );
      fixture.detectChanges();
  
      component = fixture.componentInstance;
      const data1 = {
        preferences: [1, 2, 4],
        &quot;maxCols&quot;: 5,
        &quot;selectedPref&quot;: 7
      };
      component.preferences = data1.preferences;
      component.maxCols = data1.maxCols;
      component.selectedPref = data1.selectedPref;
      expect(component.preferences).toEqual([1, 2, 4]);
  
      component.ngOnInit();
    });
  });
  </code></pre>
  <p>What I am doing wrong here?</p>