简体   繁体   中英

Angular unit test case failing while using component ngOnInit

I am using angular 7 and unable to run the unit test case while using component.ngOnInit() and fixture.detectchanges() . If remove component.ngOnInit() and fixture.detectchanges() then test case is passing but I want to detect changes after spy.

  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);
    });
  });
  });

Error Message: 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)

You should wrap your spec in async and move detectChanges inside whenStable.

  it('check getDocLibraries method is called', async(() => {
    spyOn(component, 'getDocLibraries');
    component.getDocLibraries();
    fixture.whenStable().then(()=> {
      fixture.detectChanges();
      const debugUlElement = fixture.debugElement.query(By.css('ul.sidenav-menu_sub'));
      const nativeUlElement: HTMLUListElement = debugUlElement.nativeElement;
      expect(nativeUlElement.childElementCount).toEqual(1);
    });
    component.ngOnInit();
  }));

Note: You have an extra set of '});' at the end?

You could also use fakeAsync & tick instead of async & whenStable

  it('check getDocLibraries method is called', fakeAsync(() => {
    spyOn(component, 'getDocLibraries');
    component.getDocLibraries();
    component.ngOnInit();
    tick();
    fixture.detectChanges();
    const debugUlElement = fixture.debugElement.query(By.css('ul.sidenav-menu_sub'));
    const nativeUlElement: HTMLUListElement = debugUlElement.nativeElement;
    expect(nativeUlElement.childElementCount).toEqual(1);
  }));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM