Angular 如何测试此函数的所有路径?

Angular 如何测试此函数的所有路径?,angular,unit-testing,jasmine,karma-jasmine,Angular,Unit Testing,Jasmine,Karma Jasmine,我对单元测试并不陌生,我被困在这个功能中 onPreview(value: any): void { this.showLoading('Cargando PDF...'); if (value) { this._restService.getDocument(value).subscribe( (restResult: any) => { this._dialog.open(DialogPreviewDocument,

我对单元测试并不陌生,我被困在这个功能中

onPreview(value: any): void {
    this.showLoading('Cargando PDF...');
    if (value) {
      this._restService.getDocument(value).subscribe(
        (restResult: any) => {
 
          this._dialog.open(DialogPreviewDocument, {
            data: restResult.data
          });
        },
        error => {
          Swal.fire({
            icon: 'error',
            title: 'Ocurrió un error al abrir este documento',
            confirmButtonColor: '#3085d6',
            confirmButtonText: 'Cerrar'
          }).then(r => {
          });
        }
      );
    }
  }
这是我的测试,第一个测试运行得很好,但是当我尝试测试时,当我从服务收到错误时,我收到了一个错误

it('should call onPreview with error', (done => {
        spyOn(component, 'onPreview');
        let getDocument = spyOn(service, 'getDocument')
        spyOn(Swal, 'fire')
        component.onPreview("1614210817503");
        setTimeout(() =>{
            getDocument.and.throwError("Error")
            expect(service.getDocument).toHaveBeenCalled();
            expect(Swal.fire).toHaveBeenCalled()

        })
    }));
错误:预期已调用spy getDocument。
在
在http://localhost:4200/_karma_webpack_/src/app/modules/home/components/dashboard-详细信息/仪表板详细信息.部件规格ts:468:41
在ZoneDelegate.invokeTask(http://localhost:4200/_karma_webpack_/node_modules/zone.js/dist/zone-常青树.js:399:1)
在ProxyZoneSpec.push.QpwO.ProxyZoneSpec.onInvokeTask上(http://localhost:4200/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:323:1)
错误:预期已调用间谍火力。
在
在http://localhost:4200/_karma_webpack_/src/app/modules/home/components/dashboard-详细信息/仪表板详细信息.部件规格ts:469:31
在ZoneDelegate.invokeTask(http://localhost:4200/_karma_webpack_/node_modules/zone.js/dist/zone-常青树.js:399:1)
在ProxyZoneSpec.push.QpwO.ProxyZoneSpec.onInvokeTask上(http://localhost:4200/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:323:1)
如果有人能举个例子或解释我做错了什么,我将不胜感激。
提前感谢

让getDocument抛出一个错误和让getDocument返回一个包含错误的可观察对象是两件不同的事情。您要查找的是
getDocument.and.returnValue(Observable.throw())


编辑:可能还有很多其他问题,但是由于你没有发布整个服务和整个规范文件,我不能肯定。在其他小问题中,似乎不需要超时。

这里的主要问题是,您需要知道
spyOn
的功能以及您实际要测试的内容

spyOn()
将spied方法替换为存根,这意味着它实际上不调用真正的方法

所以在您的例子中,
component.onPreview(“1614210817503”)
没有调用real方法,因为它被stub替换为
spyOn(组件“onPreview”)

为了使
spyOn()
真正调用实方法,我们使用
和.callThrough()链接它

因此,在本例中,它将是
spyOn(组件,'onPreview')。和.callThrough()

但是您甚至不必在这里监视
onPreview
方法,您只需要调用真正的方法,然后去掉内部调用的函数,以获得所需的流(例如使用
spyOn()和.returnValue()
spyOn()和.callFake()

Error: Expected spy getDocument to have been called.
        at <Jasmine>
        at http://localhost:4200/_karma_webpack_/src/app/modules/home/components/dashboard-details/dashboard-details.component.spec.ts:468:41
        at ZoneDelegate.invokeTask (http://localhost:4200/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:399:1)
        at ProxyZoneSpec.push.QpwO.ProxyZoneSpec.onInvokeTask (http://localhost:4200/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:323:1)
    Error: Expected spy fire to have been called.
        at <Jasmine>
        at http://localhost:4200/_karma_webpack_/src/app/modules/home/components/dashboard-details/dashboard-details.component.spec.ts:469:31
        at ZoneDelegate.invokeTask (http://localhost:4200/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:399:1)
        at ProxyZoneSpec.push.QpwO.ProxyZoneSpec.onInvokeTask (http://localhost:4200/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:323:1)