HttpTestingController expectOne Match URL error

I'm testing my http service class and trying to test the request, but I get this error

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

I have no idea why, I'm using the same parameters on the service and on the test class.

Service class

export class AcompanhamentoStatusService {

  constructor(private http: HttpClient) { }

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

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

Test

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();
  });
});

All tests are passing, only this one fails, any idea why this could be happening?

Upvotes: 4

Views: 9216

Answers (1)

Stefano Borz&#236;
Stefano Borz&#236;

Reputation: 2476

I got the same problem, I commented the analog three lines of your code in my project and I got the real URL that my code used, so these:

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

Then, I got the current URL and I understood which was the mismatch.

Upvotes: 2

Related Questions