Skip to content

HttpTestingController different behavior of the test based on place where verify() was put. #19903

@Philips7

Description

@Philips7

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

Test behaves differently based on place where verify() was put. Not always full path and unexpected requests are shown as well.

When it is put in afterEach(), sends two errors:

  1. First error for expected request with full path.
  2. Second Error for unexpected request without params.

Screen for verify() placed in afterEach():
image

When it it put in the end of test, sends one error:

  1. Error for expected request with full path, and none unexpected requests.

Screen for verify() in the end of the test:
image

Expected behavior

Test should show expected and unexpected requests with full path regardless of place where verify() was put.

Minimal reproduction of the problem with instructions

Code for bug reproduction in available on github:
https://github.com/Philips7/HttpTestingController


import {TestBed, async, inject} from '@angular/core/testing';
import {AppComponent} from './app.component';
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
import {HttpClient, HttpParams} from '@angular/common/http';

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

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      declarations: [
        AppComponent
      ],
    });
    httpMock = TestBed.get(HttpTestingController);
  }));

  it('should work fine', inject([HttpClient], (http: HttpClient) => {
    const geocodeResponse = {
      'location': {
        'entity_type': 'subzone',
        'entity_id': 97456,
        'title': 'Rzeszów',
        'latitude': '50.0499990000',
        'longitude': '22.0000000000',
        'city_id': 267,
        'city_name': 'Rzeszów',
        'country_id': 163,
        'country_name': 'Poland'
      }
    };

    const params = new HttpParams()
      .append('lat', '50.04132')
      .append('lon', '21.99901');

    http
      .get('https://developers', {params}).subscribe();

    const req = httpMock
      .expectOne(`https://developers?lat=50.04132&lng=21.99901`);
    expect(req.request.method).toEqual('GET');

    req.flush(geocodeResponse);
    
    // httpMock.verify(); Behaves differently based on place where is placed
  }));

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

What is the motivation / use case for changing the behavior?

To make tests reliable.

Environment


Angular version: 4.4.6


Browser:
- [x] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: v8.6.0  
- Platform: Windows 

Others:

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: common/httpIssues related to HTTP and HTTP Client

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions