【发布时间】:2018-05-27 12:34:12
【问题描述】:
我无法取消订阅我的测试。 还有人有这个问题吗?? (我做错什么了吗?)
基本测试:
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
【问题讨论】:
-
你是如何创建
isLoggedInSub的?你的ngOnDestroy方法是什么样的? -
isLoggedInSub: Subscription;isLoggedInSub = someObservableFunction.subscribe(() => {});ngOnDestroy() { isLoggedInSub.unsubscribe() }
标签: angular components observable unsubscribe