HttpTestingController ExpectOne匹配URL错误

时间:2018-07-18 18:39:44

标签: angular jestjs

我正在测试我的http服务类并尝试测试请求,但是我遇到了这个错误

Expected one matching request for criteria "Match URL: https://beneficios/api/v1/processobeneficio/1111111111111/timeline", found none.

我不知道为什么,我在服务和测试类上使用相同的参数。

服务等级

export class AcompanhamentoStatusService {

  constructor(private http: HttpClient) { }

  public ObterStatusResgate<T>(protocolo: string): Observable<T> {

    return this.http.get<T>(environment.apiEndpoint + 'processobeneficio/' + protocolo + '/timeline' );
  }

测试

describe('AcompanhamentoStatusService', () => {
  let service: AcompanhamentoStatusService;
  let httpMock: HttpTestingController;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [ AcompanhamentoStatusService ],
      imports: [ HttpClientTestingModule ],
    });
    service = TestBed.get(AcompanhamentoStatusService);
    httpMock = TestBed.get(HttpTestingController);

  });

  it('should have apiEndpoint in Enviroment', () => {
    expect(environment.apiEndpoint).toBeDefined();
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
    expect(service).toBeDefined();
  });

  it('Deve retornar status acompanhamento', () => {
    const protocolo = '1111111111111';
    const mockPassos = [
      { status: 'Em Análise', data: '20/08/2018'},
      { status: 'Pago', data: '25/08/2018'},
    ];

    service.ObterStatusResgate(protocolo).subscribe( passos => {
      expect(passos).toBe(mockPassos);
    });

    const req = httpMock.expectOne(environment.apiEndpoint + 'processobeneficio/' + protocolo + '/timeline' );
    expect(req.request.method).toBe('GET');
    req.flush(mockPassos);
  });

  afterEach(() => {
    httpMock.verify();
  });
});

所有测试都通过了,只有一项失败了,不知道为什么会发生这种情况吗?

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我评论了项目中的三行模拟代码,并得到了我的代码使用的真实URL,因此:

const req = httpMock.expectOne(environment.apiEndpoint + 'processobeneficio/' + protocolo + '/timeline' );
expect(req.request.method).toBe('GET');
req.flush(mockPassos);

然后,我得到了当前的 URL ,并且我知道哪个是不匹配的。