2017-12-13 8 views
-1

私のテストでは退会できません。 他に誰かがこの問題を抱えていますか? (私は間違って何をやっている?)ngOnDestroy()内の登録を解除できません角度5

基本テスト:

describe('ngOnDestroy()',() => { 
    it('should unsubscribe if isLoggedInSub is defined', async(() => { 
    comp.ngOnInit(); 
    fixture.detectChanges(); 
    comp.ngOnDestroy(); 
    fixture.detectChanges(); 
    expect(comp.isLoggedInSub).not.toBeDefined(); 
    })); 
}); 

コンソールエラー:

Uncaught Expected Subscriber({ closed: true, _parent: null, _parents: null, _subscriptions: null, syncErrorValue: null, syncErrorThrown: false, syncErrorThrowable: false, isStopped: true, destination: SafeSubscriber({ closed: false, _parent: null, _parents: null, _subscriptions: null, syncErrorValue: null, syncErrorThrown: false, syncErrorThrowable: false, isStopped: false, destination: Object({ closed: true, next: Function, error: Function, complete: Function }), _parentSubscriber: <circular reference: Object>, _context: <circular reference: Object>, _next: Function, _error: undefined, _complete: undefined }) }) not to be defined. 
at Object.testing_3.async 
+1

どのようにあなたが 'isLoggedInSub'を作成しているだろうか?あなたの 'ngOnDestroy'メソッドはどのように見えますか? – vincecampanale

+0

'isLoggedInSub:サブスクリプション;' 'isLoggedInSub = someObservableFunction.subscribe(()=> {});' 'ngOnDestroy(){isLoggedInSub.unsubscribe()}' – cuznerdexter

答えて

0

あなたは主題を持っていて、直接、上記のいずれかで退会しようとしている場合メソッドの場合、この種のエラーに直面します。 例:別のコンポーネントは、件名への参照を持っているよう

//AppComponent.ts

export class AppComponent implements OnDestroy{ 
appSubject:new Subject(); 
appSubject.subscribe(()=>...) 
ngOnDestroy() { 
    appSubject.unsubscribe(); // If your subject is passing the next value from a 
         different component 
} 
} 

これは通常、このエラーが発生します。

それを行うには良い方法は

export class AppComponent implements OnDestroy{ 
appSubject:new Subject(); 
$subjectSubscription:Subscription = appSubject.subscribe(()=>...) 
ngOnDestroy() { 
    $subjectSubscription.unsubscribe(); 
} 
} 
+0

おかげ。これは私がすでにやっていることですが、退会者はまだ働いていません。 – cuznerdexter

+0

あなたが退会しようとしているスペックファイルの大部分を共有してください。 –

関連する問題