проверить HTTP-вызов внутри обещания с жасмином - PullRequest
0 голосов
/ 28 февраля 2020

Я изучаю angular и юнит-тестирование и не могу решить эту проблему. Возможно, я все делаю неправильно, но я хочу проверить, что был сделан http-вызов.

app.service.ts:

transformData(data):Promise<any>{
    return new Promise((resolve, reject) => {
          this.http.post<any>('https://www.example.com',data).subscribe(
        resp => {
         resolve(resp);
         },
        err => {
         reject(err);
         }
      );

       });
    }

мой тест сейчас:

fit("should submit data for processing", fakeAsync(() => {

    const service = TestBed.get(AppService);

    let response = {
      processedData: 100
    };

    service
    .transformData({'data':'data'})
    .then(result => {
      expect(result).toEqual(response);
    });

   // Expect a call to this URL
  const req = httpTestingController.expectOne(
  "https://www.example.com/"
    );
  expect(req.request.method).toEqual("POST");
  req.flush(response);
  tick();

  }));

Там написано:

Error: Expected one matching request for criteria "Match URL: https://www.example.com/", found none.

1 Ответ

0 голосов
/ 28 февраля 2020

Я бы проверил это так:

fit("should submit data for processing", async(done) => {

    const service = TestBed.get(AppService);

    let response = {
      processedData: 100
    };

    service
    .transformData({'data':'data'})
    .then(result => {
      expect(result).toEqual(response);
      // call done() to tell the test we are done with our assertions
      done();
    });

    // Expect a call to this URL
    const req = httpTestingController.expectOne("https://www.example.com");
    expect(req.request.method).toEqual("POST");
    req.flush(response);
  });
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...