jeudi 6 février 2020

Angular 8 HttpTestingController error - 'Cannot flush a cancelled request.'

I am having a real problem with a test of mine whereas an HTTP call I have had successfully working and mocked and returned is now not flushing from within the test and causing the test to fail. The error I am getting is

Cannot flush a cancelled request.

The actual test itself is using Jest/Cucumber within the default testing framework and this is all working correctly so the issue does not lie there.

The 'then' part of my test is as follows:

then(
      /^the payment authority should be sent as '(.*?)'$/,
      fakeAsync(consent => {
        tick();
        flushBrandPropertyCall(httpTestingController);
        flushQuoteStatusCall(httpTestingController);
        flushReferenceDataCall(httpTestingController);
        httpTestingController.verify(); //Similar to an assert just make sure page is stable before continuing

        const submitBtn = fixture.debugElement.query(By.css('#continue-button')).nativeElement;
        submitBtn.click();

        const timeRequest = httpTestingController.expectOne(HttpUrl.Time);
        console.log('Cancelled?', timeRequest.cancelled);
        if (!timeRequest.cancelled) timeRequest.flush({ now: Date.now() });
        flushBrandPropertyCall(httpTestingController);
        tick(1500); // Not sure exact source - maybe modal messages?
        const payRequest = httpTestingController.expectOne(HttpUrl.SinglePayment);
        expect(payRequest.request.body.paymentAuthority).toEqual(consent);
        payRequest.flush({ paymentStatus: 'CARD_PAYMENT_SUCCESS', authCode: '', policyNumber: 5555, quoteVersion: 20 });
        httpTestingController.verify();
        expect(true).toEqual(true);
      })
    );
  });

And the HTTP call that is failing is being cancelled is flushBrandPropertyCall) which is as follows:

export const flushBrandPropertyCall = (controller: HttpTestingController) => {
  const request = controller.expectOne('/web-services/rest/properties/removedForSecurity');
  console.log(request)

  request.flush([
    {
      id: 8,
      brand: 'removedForSecurity',
      key: 'openingTimes',
      value:
        '{"motor":{"SAT":{"from":"09:00","to":"18:00"},"MON":{"from":"08:00","to":"22:00"},"TUE":{"from":"08:00","to":"22:00"},"WED":{"from":"08:00","to":"22:00"},"THU":{"from":"08:00","to":"22:00"},"FRI":{"from":"08:00","to":"22:00"},"SUN":{"from":"10:00","to":"18:00"},"YYYY-12-25":"holiday","YYYY-12-26":"holiday","2017-01-02":{"from":"10:00","to":"18:00"}},"home":{"SAT":{"from":"09:00","to":"17:00"},"SUN":{"from":"10:00","to":"16:00"},"WEEKDAY":{"from":"08:00","to":"22:00"},"YYYY-12-25":"holiday","YYYY-12-26":"holiday","2017-01-02":{"from":"10:00","to":"18:00"}},"multicover":{"SAT":{"from":"09:00","to":"17:00"},"SUN":{"from":"10:00","to":"16:00"},"WEEKDAY":{"from":"08:00","to":"22:00"},"YYYY-12-25":"holiday","YYYY-12-26":"holiday","2017-01-02":{"from":"10:00","to":"18:00"}},"van":{"SAT":{"from":"09:00","to":"17:00"},"MON":{"from":"08:00","to":"21:00"},"TUE":{"from":"08:00","to":"21:00"},"WED":{"from":"08:00","to":"21:00"},"THU":{"from":"08:00","to":"21:00"},"FRI":{"from":"08:00","to":"21:00"},"SUN":{"from":"10:00","to":"16:00"},"YYYY-12-25":"holiday","YYYY-12-26":"holiday","2017-01-02":{"from":"10:00","to":"18:00"}}}'
    },
    {
      id: 364,
      brand: 'aaaaa',
      key: 'csOpeningTimes',
      value:
        '{"motor":{"SAT":{"from":"09:00","to":"17:00"}, "SUN":{"from":"10:00","to":"16:00"},"MON":{"from":"08:00","to":"20:00"},"TUE":{"from":"08:00","to":"20:00"},"WED":{"from":"08:00","to":"20:00"},"THU":{"from":"08:00","to":"20:00"},"FRI":{"from":"08:00","to":"20:00"}},"home":{"SAT":{"from":"09:00","to":"17:00"},"SUN":{"from":"10:00","to":"16:00"},"MON":{"from":"08:00","to":"20:00"},"TUE":{"from":"08:00","to":"20:00"},"WED":{"from":"08:00","to":"20:00"},"THU":{"from":"08:00","to":"20:00"},"FRI":{"from":"08:00","to":"20:00"}},"van":{"SAT":{"from":"09:00","to":"17:00"},"SUN":{"from":"10:00","to":"16:00"},"MON":{"from":"08:00","to":"21:00"},"TUE":{"from":"08:00","to":"21:00"},"WED":{"from":"08:00","to":"21:00"},"THU":{"from":"08:00","to":"21:00"},"FRI":{"from":"08:00","to":"21:00"}}}'
    },
    {
      id: 91,
      brand: 'aaaaaa',
      key: 'publicKey',
      value:
        '10001|AD0FA4B69A67FA53084A6208E9E248D33EC1251C1B091893CA7855672755352A44C372F6999CEA01BD9B34F4A08E64A1A8009E1596E4B992FCCB6888E721E6CF1DA3F5918DCDB7F9B461823CCACF867671B32CE487236D24BEB93CE448E9541537CC18668B0CCE60E9566D5E00F62C1636606ABCBA38DADFF9A898D37388852D698A27388849A4C5A9394233E55B14541A811C228888239A7B55C9F782F0C0EE56D283AA74F11025AF0E3A08C8CA545505853F0060D61F003DAD78B57EE53C86395EB6BF013CA2C85FD9B4E21C062770BFDC02EA3315CD60FC4ED8F83E4108B8479BB0DA2ADC6E84029026FBB4ACEB123220C174DE7821A01EAA2E98D3342E03'
    }
  ]);
  tick();
};

I have tried to have a look around online but I can't for the life of me find any info on that error. Does anyone know why a request would be cancelled from within a test spec using HttpTestingController?

I have also console.logged the requestOne and here is the request object

 TestRequest {
      request: HttpRequest {
        url: '/web-services/rest/properties/removedForSecurity',
        body: null,
        reportProgress: false,
        withCredentials: false,
        responseType: 'json',
        method: 'GET',
        headers: HttpHeaders {
          normalizedNames: Map {},
          lazyUpdate: null,
          headers: Map {}
        },
        params: HttpParams {
          updates: null,
          cloneFrom: null,
          encoder: HttpUrlEncodingCodec {},
          map: null
        },
        urlWithParams: '/web-services/rest/properties/removedForSecurity'
      },
      observer: InnerSubscriber {
        closed: true,
        _parentOrParents: null,
        _subscriptions: null,
        syncErrorValue: null,
        syncErrorThrown: false,
        syncErrorThrowable: false,
        isStopped: true,
        destination: {
          closed: true,
          next: [Function: next],
          error: [Function: error],
          complete: [Function: complete]
        },
        parent: MergeMapSubscriber {
          closed: true,
          _parentOrParents: null,
          _subscriptions: null,
          syncErrorValue: null,
          syncErrorThrown: false,
          syncErrorThrowable: true,
          isStopped: true,
          destination: [FilterSubscriber],
          project: [Function],
          concurrent: 1,
          hasCompleted: true,
          buffer: [],
          active: 1,
          index: 1
        },
        outerValue: undefined,
        outerIndex: undefined,
        index: 1
      },
      _cancelled: true
    }

Aucun commentaire:

Enregistrer un commentaire