I am getting TypeError: cannot read property 'id' of undefined when trying to perform unit test in my angular 8 application.
Here is my template
<h4>{{student.id}}</h4>
I am getting TypeError: cannot read property 'id' of undefined when trying to perform unit test in my angular 8 application.
Here is my template
<h4>{{student.id}}</h4>
Here is my ponent
import {Component, EventEmitter, Input, OnInit} from '@angular/core';
import { Student } from '@app/app-types';
@Component({
...... saved some typing
})
export class StudentComponent implements OnInit {
@Input() student: Student;
refreshForm: EventEmitter<any>;
constructor(){}
ngOnInit(){
this.refreshForm = new EventEmitter();
}
}
and here is the unit test
import {async, ComponentFixture, TestBed } from '@angular/core/testing';
import {StudentComponet} from './student-ponent';
describe('StudentComponet', () => {
let ponent: StudentComponent;
let fixture: ComponentFixture<StudentComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [StudentComponent]
}).pileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(StudentComponent);
ponent = fixture.ponentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(ponent).toBeTruthy();
})
});
Share
Improve this question
asked Jul 30, 2019 at 5:44
sage poudelsage poudel
3574 silver badges14 bronze badges
2
- Is student one time instanciated ? – FrV Commented Jul 30, 2019 at 5:51
- @FrV Yes. The student is received from the parent ponent via ngrx/store . – sage poudel Commented Jul 30, 2019 at 5:55
1 Answer
Reset to default 6use
student?.id
in unit test you have to provide value for student
beforeEach(() => {
...
ponent.student = value;
fixture.detectChanges();
);