如何使用Jasmine(Angular)轻松模拟服务HT​​TP请求?

时间:2019-10-17 17:05:11

标签: angular unit-testing jasmine karma-jasmine angular-testing

为什么这个间谍无法工作?我正在创建处方服务的实例并监视fetchClientPrescriptions方法,但是当我检查其是否被调用时,出现错误。但是getClientPrescriptions的第一个间谍工作正常。

测试:

  let prescriptions = [
    {"ndcpackage": "58160082552", "form": "1.0ML Syringe", "name": "Havrix", "dosage": "Havrix INJ 720UNIT", "quantity": "3", "refill_freq": 30},
    {"ndcpackage": "59310058020", "form": "1.0EA Box", "name": "Proair Respiclick", "dosage": "Proair Respiclick AER", "quantity": "0", "refill_freq": 30}
  ];

  it('should fetch client prescriptions if clientId is provided', async(() => {
    let spyC = spyOn(component, 'getClientPrescriptions');
    let spyS = spyOn(prescriptionService, 'fetchClientPrescriptions').and.returnValue(of(prescriptions));
    component.clientId = 5;
    component.ngOnInit();
    fixture.whenStable().then(() => {
      expect(spyC).toHaveBeenCalled();
      expect(spyS).toHaveBeenCalled();
      expect(component.currentPrescriptions).toEqual(prescriptions);
    });
  }));

服务:

  fetchClientPrescriptions(id: any) {
    return this.http.get<DrugSelection[]>(environment.apiURL + '/fetch-client-rx/' + id);
  }

组件:

  ngOnInit() {
    if (this.clientId != null) {
      this.getClientPrescriptions();
    }
  }

  getClientPrescriptions() {
    this.prescriptionService.fetchClientPrescriptions(this.clientId).subscribe(p => {
      this.dataSource = new MatTableDataSource<DrugSelection>(p);
      this.dataSource.sort = this.sort;
      this.currentPrescriptions = p;
    });
  }

错误:

Error: Expected spy fetchClientPrescriptions to have been called.
        at <Jasmine>
        at http://localhost:9876/_karma_webpack_/src/app/client/client-prescriptions/client-prescriptions.component.spec.ts:48:20
        at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/C:/Users/BHanna/Documents/my-mfg/mymfg-spring-agents/src/main/webapp/node_modules/zone.js/dist/zone.js:396:1)
        at AsyncTestZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/C:/Users/BHanna/Documents/my-mfg/mymfg-spring-agents/src/main/webapp/node_modules/zone.js/dist/async-test.js:102:1)
    Error: Expected $.length = 0 to equal 2.
    Expected $[0] = undefined to equal Object({ ndcpackage: '58160082552', form: '1.0ML Syringe', name: 'Havrix', dosage: 'Havrix INJ 720UNIT', quantity: '3', refill_freq: 30 }).
    Expected $[1] = undefined to equal Object({ ndcpackage: '59310058020', form: '1.0EA Box', name: 'Proair Respiclick', dosage: 'Proair Respiclick AER', quantity: '0', refill_freq: 30 }).
        at <Jasmine>
        at http://localhost:9876/_karma_webpack_/src/app/client/client-prescriptions/client-prescriptions.component.spec.ts:49:46
        at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/C:/Users/BHanna/Documents/my-mfg/mymfg-spring-agents/src/main/webapp/node_modules/zone.js/dist/zone.js:396:1)
        at AsyncTestZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/C:/Users/BHanna/Documents/my-mfg/mymfg-spring-agents/src/main/webapp/node_modules/zone.js/dist/async-test.js:102:1)

1 个答案:

答案 0 :(得分:2)

在间谍let spyC = spyOn(component, 'getClientPrescriptions');中,您正在设置一个间谍,但是该间谍仅拦截该呼叫,并且不会进一步进行呼叫。您必须像这样完成它:

    let spyC = spyOn(component, 'getClientPrescriptions').and.callTrough();

通过这种方式调用实际方法,然后调用fetchClientPrescriptions方法