我正在为我的组件编写单元测试,但是在创建组件实例并显示以下错误时遇到了麻烦:
TypeError: Cannot read property 'patientId' of null
我尝试模拟所有提供程序,包括路由器和活动路由器 这是我调用该组件的方式:
public onSelectedOrganizationInsurance(organizationInsurance: OrganizationInsurance) {
this.router.navigate(['../search-insurance-plan'],{
relativeTo: this.activatedRoute,state: {
selectedOrganizationInsurance: organizationInsurance,patientId: this.patientId
}
});
我的component.ts是
export class PatientInsurancePlanSearchComponent implements OnInit {
private patientId: number = -1;
public selectedOrganizationInsurance: OrganizationInsurance;
public organizationInsurancePlans$: Observable<OrganizationInsurancePlan[]>;
constructor(
private router: Router,private activatedRoute: activatedRoute,private biilingHttpService: BillingHttpService
) {
this.selectedOrganizationInsurance = new OrganizationInsurance();
}
ngOnInit() {
this.patientId = history.state.patientId as number;
this.selectedOrganizationInsurance = history.state.selectedOrganizationInsurance as OrganizationInsurance;
this.organizationInsurancePlans$ = this.biilingHttpService.getOrganizationInsurancePlans(this.selectedOrganizationInsurance.id);
}
spec.ts
class FakeInsurancePlanSearchComponent {
@Input() public organizationInsurancePlans: OrganizationInsurancePlan[] = [];
@Input() public selectedOrganizationInsurance: OrganizationInsurance;
}
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ PatientInsurancePlanSearchComponent,FakeInsurancePlanSearchComponent ],imports: [
StoreModule.forRoot({}),HttpClientModule,RouterTestingModule,],providers: [
Store,{
provide: activatedRoute,useValue: {
state: of({ selectedorganizationInsurancePlan: 'AETNA'})
}
},BillingHttpService
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(PatientInsurancePlanSearchComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create',() => {
expect(component).toBeTruthy();
});
请指导我所缺少的内容。