.detectChanges() not working within Angular test

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

    The answer provided here https://stackoverflow.com/a/50142134/3765819 fixes the problem. However, there is also another way which I think it can prevent further problems on the UI. The problem I had was similar to the one described on the question, meaning that when testing for a specific string on HTML I could not find it. Even though when running the code it worded fine, the UI was not updated accordingly when testing it.

    What I had to do was:

    To inject ChangeDetectorRef into the .ts file:

    constructor(private changeDetector: ChangeDetectorRef) {}
    

    and call it when needed:

    this.changeDetector.markForCheck();
    

提交回复
热议问题