.detectChanges() not working within Angular test

前端 未结 4 753
梦毁少年i
梦毁少年i 2021-02-07 09:23

I\'ve been tasked with writing tests for a chat app developed with Angular. Below is the snippet of Angular template code I\'m currently writing tests for:



        
4条回答
  •  一生所求
    2021-02-07 09:32

    I know this question is old, but I recently had this same issue where a spinner would constantly spin on the Karma page because change detection only occurred once. The fix for me is whether to call fixture.detectChanges(true) or fixture.autoDetectChanges(true).

    beforeEach(() => { 
      fixture = TestBed.createComponent(TestComponent);
      component = fixture.componentInstance;
      component.titleInputEdit = true
      // 'detectChanges' will only test for onPush events: 
      // fixture.detectChanges();
    
      // 'autoDetectChanges' will continually check for changes until the test is complete.  
      // This is slower, but necessary for certain UI changes
      fixture.autoDetectChanges(true);
    
      debugElement = fixture.debugElement;
    }); 
    

提交回复
热议问题