0

I have a an Angular app with a service file that constructs the URL for API calls and unpacks the returned JSON. All of the methods in the file are variations on this theme:

  getExampleReport(skip: number, take: number, gridSort: SortDescriptor[]) {    
    let queryString = '?Skip=' + skip + '&Take=' + take;
    if(gridSort && gridSort.length > 0 && gridSort[0].dir && gridSort[0].field) {
      queryString = queryString + '&SortField=' + gridSort[0].field + '&SortDirection=' + gridSort[0].dir;
    }

    return this.httpClient
      .get<ExampleReportDisplayModel>(
        this.globals.ApiUrl + '/Path/To/API/ExampleReport' + queryString
      )
      .pipe(
        map(data => {
          if (data !== null) {
            return this.ExampleReportMapper.mapToModel(data);
          }
        })
      );
  }

I have a corresponding .spec.ts file where all of those methods are tested.

  it('getExampleReport should return data', () => {
    spyOnProperty(globalService, 'ApiUrl').and.returnValue('http://testUrl');
    const responseData = {
      totalCount: 1,
      reportItems: [
        {
          itemNumber: 1,
          itemStatus: 'TEST',
          accountNumber: '111111111',
          state: 'AA',
          customerName: 'Test McTest',
          processorName: 'Test MacTest'
        }]
    };
    var sort: SortDescriptor[] = [
      {
        dir: "asc",
        field: "itemStatus"
      }
    ];
    service.getExampleReport(0, 1, sort).subscribe(
      (data) => {
        expect(data.totalCount).toBe(1);
      }
    );
    const httpRequest = httpMock.expectOne('http://testUrl/Path/To/APIExampleReport?Skip=0&Take=1&SortField=itemStatus&SortDirection=asc');
    expect(httpRequest.request.method).toBe('GET');
    httpRequest.flush(responseData);

    httpMock.verify();
  });

Some of these tests work as I expect, but several of them are persistently giving me this error message.

1) getExampleReport should return data
     ReportsService
     Error: Expected one matching request for criteria "Match URL: http://testUrl/Path/To/APIExampleReport?Skip=0&Take=1&SortField=itemStatus&SortDirection=asc", found none.
    at HttpClientTestingBackend.expectOne (http://localhost:9876/_karma_webpack_/node_modules/@angular/common/fesm2015/http/testing.js:487:1)
    at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/workflows/example_workflow/services/reports.service.spec.ts:122:34)
    at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:359:1)
    at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:308
    at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:358:1)
    at Zone.run (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:124:1)
    at runInTestZone (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:561:1)
    at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:576:1)
    at <Jasmine>
     Error: Expected no open requests, found 1: GET http://testUrl/Path/To/APIExampleReport
    at HttpClientTestingBackend.verify (http://localhost:9876/_karma_webpack_/node_modules/@angular/common/fesm2015/http/testing.js:538:1)
    at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/workflows/example_workflow/services/reports.service.spec.ts:29:14)
    at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:359:1)
    at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:308
    at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:358:1)
    at Zone.run (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:124:1)
    at runInTestZone (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:561:1)
    at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:576:1)

This error confuses me. I see that it isn't seeing the GET request expected by httpMock.expectOne(...); and that it is seeing a response when it expects none from httpMock.verify();. I added logging to the service method and confirmed that the method is being called and that queryString is building the exact string I expect. Even more curious, these tests are passing on a remote build server, but some of them (not all!) of them fail for me locally.

Why is my test not passing?

MackM
  • 2,906
  • 5
  • 31
  • 45
  • How about the answers on this question - https://stackoverflow.com/questions/50755939/angular-5-jasmine-error-expected-one-matching-request-for-criteria-found-none – Mauricio Gracia Gutierrez Jan 27 '22 at 21:41
  • I see a similar problem in this question, but the answer is to use `.expectOne()` with a url string, which I am already doing. https://stackoverflow.com/questions/52206486/angular-w-jest-verify-vs-expectone – MackM Jan 27 '22 at 21:45
  • 1
    @MauricioGraciaGutierrez Thank you for the reference. Unfortunately I don't see any answers that help in my situation. The answers there are 1) Add the parameters to the expected URL (I have done this) 2) Add logging to see what query string is created, (I have done this and it matches the URL I expect) 3) Handling asynchronous services. I do not fully understand the third group of answers but as I am seeing the full query string logged from the report service, I do not believe that is my issue. – MackM Jan 27 '22 at 21:52

0 Answers0