我正在使用 angular 7 并且在使用 and 时无法运行单元component.ngOnInit()
测试用例fixture.detectchanges()
。如果删除component.ngOnInit()
然后fixture.detectchanges()
测试用例通过但我想检测间谍后的变化。
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [AppTestingModule, EffectsModule.forRoot([NodeEffects])],
declarations: [SidenavComponent],
schemas: [NO_ERRORS_SCHEMA],
providers: [ApiNodesService]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SidenavComponent);
component = fixture.componentInstance;
browsingService = TestBed.get(BrowsingFilesService);
apiNodesService = TestBed.get(ApiNodesService);
appConfig = TestBed.get(AppConfigService);
// appConfigSpy = spyOn(appConfig, 'get').and.returnValue([navItem]);
});
it('check getDocLibraries method is called', () => {
spyOn(component, 'getDocLibraries');
fixture.detectChanges();
component.ngOnInit();
component.getDocLibraries();
fixture.whenStable().then(data => {
const debugUlElement = fixture.debugElement.query(By.css('ul.sidenav-menu_sub'));
const nativeUlElement: HTMLUListElement = debugUlElement.nativeElement;
expect(nativeUlElement.childElementCount).toEqual(1);
});
});
});
错误信息:
TypeError: Cannot convert undefined or null to object
at Function.keys (<anonymous>)
at SidenavComponent../src/app/components/sidenav/sidenav.component.ts.SidenavComponent.buildMenu (http://localhost:9876/src/app/components/sidenav/sidenav.component.ts?:93:19)
at SidenavComponent../src/app/components/sidenav/sidenav.component.ts.SidenavComponent.ngOnInit (http://localhost:9876/src/app/components/sidenav/sidenav.component.ts?:76:28)
at UserContext.<anonymous> (http://localhost:9876/src/app/components/sidenav/sidenav.component.spec.ts?:103:15)