【发布时间】:2019-05-03 01:24:31
【问题描述】:
我正在尝试测试是否调用了订阅取消订阅方法并且出现错误。
我的组件如下所示:
import { Component, OnInit } from '@angular/core'; import {
Subscription } from 'rxjs/Subscription'; import { LoaderService } from
'@core/components/loaders/services/loader.service';
@Component({ selector: 'loader-mask', templateUrl:
'./loader-mask.component.html', styleUrls:
['./loader-mask.component.css'] })
export class LoaderMaskComponent implements OnInit {
subscription: Subscription;
ngOnDestroy() {
this.subscription.unsubscribe();
}
}
我的测试看起来像这样
it('should call unsubscribe() on ngOnDestroy', ()
=> {
let spy = spyOn(component['subscription'], 'unsubscribe').and.callFake( () => {});
component.ngOnDestroy();
expect(spy).toHaveBeenCalled(); });
但是我收到了这个错误
Error: <spyOn> : could not find an object to spy upon for unsubscribe()
【问题讨论】:
-
组件调用
this.subscription.unsubscribe(),你测试component['wizardSubscription'].unsubscribe... -
也不应该从取消订阅回调中调用取消订阅。
标签: angular typescript angular-test