簡體   English   中英

如何使用 Jasmine(Angular)輕松模擬服務 HTTP 請求?

[英]How to easily mock a service HTTP request with Jasmine (Angular)?

為什么這個間諜不工作? 我正在創建一個處方服務的實例並監視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)

在 Spy 中let spyC = spyOn(component, 'getClientPrescriptions'); ,您正在設置一個間諜,但該間諜僅攔截呼叫並且不會進一步進行。 你必須像這樣完成它:

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

這樣調用實際的方法,然后調用fetchClientPrescriptions方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM