admin管理员组文章数量:1435287
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();
);
本文标签: javascriptTypeError cannot read property 39id39 of undefinedAngular 8 TestingStack Overflow
版权声明:本文标题:javascript - TypeError: cannot read property 'id' of undefined - Angular 8 Testing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745650417a2668395.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论