.detectChanges() not working within Angular test

前端 未结 4 751
梦毁少年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:30

    It turns out this is due to using ChangeDetectionStrategy.OnPush in the component. Using OnPush only allows you to call .detectChanges() one time, so subsequent calls will fail to do anything. I'm not familiar enough with Angular to fully understand why.

    I was able to produce the required behaviour by overriding the ChangeDetectionStrategy in my TestBed configuration.

    TestBed.configureTestingModule({
        imports: [],
        declarations: [TestComponent],
        providers: []
      })
        .overrideComponent(TestComponent, {
          set: { changeDetection: ChangeDetectionStrategy.Default }
        })
        .compileComponents();
    

提交回复
热议问题