我正在尝试测试是否调用了订阅取消订阅方法,并且得到了一个错误。
我的组件看起来如下:
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()发布于 2019-01-08 12:14:03
const spy = spyOn(
(component as any).subscription,
'unsubscribe'
).and.callThrough()
component.ngOnDestroy();
expect(spy).toHaveBeenCalled();试试看,我也遇到了同样的问题,据我所知,我使用了上述方法来解决这个问题。请查证。
发布于 2019-05-02 17:24:31
component.subscription = new Subscription();
const subscription = spyOn(component.subscription, 'unsubscribe');
component.ngOnDestroy();
expect(subscription).toHaveBeenCalledTimes(1);https://stackoverflow.com/questions/54087355
复制相似问题