我在进行 Angular 8 应用程序的单元测试时,遇到了 TypeError: cannot read property 'id' of undefined 错误。
以下是我的模板:
这是我的组件。
以下是我的模板:
<h4>{{student.id}}</h4>
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();
}
}
这里是单元测试
import {async, ComponentFixture, TestBed } from '@angular/core/testing';
import {StudentComponet} from './student-component';
describe('StudentComponet', () => {
let component: StudentComponent;
let fixture: ComponentFixture<StudentComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [StudentComponent]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(StudentComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
})
});