javascript - TypeError: cannot read property 'id' of undefined - Angular 8 Testing - Stack Overflow

I am getting TypeError: cannot read property 'id' of undefined when trying to perform unit te

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
Add a ment  | 

1 Answer 1

Reset to default 6

use

student?.id

in unit test you have to provide value for student

beforeEach(() => {
  ...
  ponent.student = value;
  fixture.detectChanges();
);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745650418a4638230.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信