2017-12-13 10 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

것이다 할까? '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 
} 
} 

이 보통이 오류가 발생합니다.

이 더 좋은 방법은 어떻게`isLoggedInSub`를 만드는

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

감사합니다. 이것은 내가 이미하고있는 일이지만 탈퇴가 아직 작동하지 않습니다. – cuznerdexter

+0

좋아, 우리가 도울 수 있도록 구독 취소하려고하는 spec 파일의 plunker를 공유해주십시오. –