httpClientTestingModule mock

时间:2017-10-23 06:58:51

标签: angular unit-testing jasmine mocking

我有功能

delete(event, envName) {
event.stopPropagation();
this.toogleBtn(event);
this.modal.show('Confirm',  envName ).then(() => {
  this.http.delete(myAddress + envName, {
    headers: new HttpHeaders().set('Authorization', `Bearer token}`)
  }).subscribe(response => {
    this.getDetails().then(res => {
      this.details = res;
    });
  });
});
}

我希望使用httpClientTestingModule来模拟http请求,如:

it('should delete', () => {
  const event = new Event('click');
  const envName = 'env';
  spyOn(component, 'toogleBtn');
  spyOn(event, 'stopPropagation');
  spyOn(component.modal, 'show').and.returnValue(Promise.resolve());
  component.deleteEvnBtn(event, envName);
  const req = httpClientMock.expectOne(myAddress + envName);
  req.flush('data');
});

但我在httpClientMock.expectOne:

的行中收到错误

错误:预期一条符合条件的匹配请求“Match URL:myAddressWithEnvName”,找不到。

其中address与函数中的地址完全相同。

1 个答案:

答案 0 :(得分:0)

您的测试应为async或未调用then方法。