Testing Angular component with unsubscribe Error during cleanup of component

后端 未结 10 1648
梦毁少年i
梦毁少年i 2021-01-30 10:37

I\'m testing a component which subscribe router params. Every test pass and everything works fine. But if I look in the console, I can see an error:

Error

相关标签:
10条回答
  • 2021-01-30 11:20

    Well in my case the error was in the template. There was error in the child component ngDestroy ,which wasn't getting destroyed as i was trying to set readonly property. It would be worth your time checking your child components whether they are getting destroyed properly.

    0 讨论(0)
  • 2021-01-30 11:21

    Adding to @David Brown's response the code below is what worked for me.

          .subscribe(res => {
              ...
            },
            error => Observable.throw(error)
          )
    
    0 讨论(0)
  • 2021-01-30 11:24

    You need to refactor your method ngOnDestroy as below :

    ngOnDestroy() {
      if ( this.routeSubscription)
        this.routeSubscription.unsubscribe();
    }

    0 讨论(0)
  • 2021-01-30 11:24

    You have to do 2 things, to solve this error.

    1- add fixture.detectChanges(); in beforeEach()
    2 - you need to add below, so that component can be clear.

    afterEach(() => {
            fixture.destroy();
          });
    
    0 讨论(0)
提交回复
热议问题