我有一个依赖于Route Resolve数据的组件。 以下是路由器的代码片段
{
path: 'attachment',
component: SgCreateAttachmentComponent,
resolve: {
referenceData: SgCreateAttachmentResolve
},
data: {
'seq' : 5
},
canActivate: [SgCreateCanActivateGaurd]
}
在组件内部,我在ngOnInit中获取解析数据。
ngOnInit() {
this.standardAttachmentCategories = this._activatedRoute.snapshot.data['referenceData'].json();
}
我正在尝试对组件的ngOnInit函数进行单元测试。以下是我的规范中的代码段。
it('should ensure ngOnInit is called', () => {
fixture.detectChanges();
});
另外,我有以下配置的试验台
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [HttpModule, SgCreateModule],
providers: [FormBuilder, { provide: SgCreateService, useClass: SgCreateMockService },
{ provide: SgCreateAttachmentService, useClass: SgCreateAttachmentServiceMock },
SgCreateModelService,
{ provide: Router, useClass: RouterStub },
{ provide: ActivatedRoute, useClass: ActivatedRouteStub },
{ provide: XHRBackend, useClass: MockBackend }
]
})
.compileComponents();
fixture = TestBed.createComponent(SgCreateAttachmentComponent);
comp = fixture.componentInstance;
}));
但是,执行上述测试会引发错误:
无法读取未定义
的属性'referenceData'我也尝试了以下
it('should ensure ngOnInit is called', () => {
const activatedRouteStub = fixture.debugElement.injector.get(ActivatedRoute);
activatedRouteStub.testParams = { data: new Response() };
fixture.detectChanges();
});
但我面临同样的问题。