简体   繁体   中英

Unit Test Angular Http Request with pipe

I have to perform a unit test on this observable in which a map is found to transform the data to the required

getAll(): Observable<any> {
    return this.http.get(this.helpers.getBasicEndPoint('/configuraciones/nominas/procesos'))
    .pipe(
      /** Ordenar data por tippro */
      map((data: Procesos[]) => data = this.helpers.sortData(data, 'tippro'))
    );
  }

I have done the following it to test it but the call after the map is skipped

it('Should return array of Educational Levels', waitForAsync ((done: DoneFn) => {
    
    const mockResult: any = [
      {
          "desniv": "Basica",
          "codley": null,
          "codniv": "6"
      },
      {
          "desniv": "Universitario",
          "codley": null,
          "codniv": "4"
      },
      {
          "desniv": "Bachiller",
          "codley": null,
          "codniv": "5"
      },
      {
          "desniv": "Primaria",
          "codley": null,
          "codniv": "1"
      },
      {
          "desniv": "Secundaria",
          "codley": "SES",
          "codniv": "2"
      }
    ]

    expect(mockResult.length).toBeGreaterThan(1)

    getHttpClientSpy.get.and.returnValue(of(mockResult));

    procesosService.getAll()
      .subscribe((resp) => {
        expect(resp).toEqual(mockResult);
        map((data: Procesos[]) => data = helpers.sortData(data, 'tippro'))       
        done();
      });
  }));

this piece of code is expected to be read

enter image description here

I am not sure why that line is not being covered, it should be covered.

Follow the comments with:!:

it('Should return array of Educational Levels', waitForAsync ((done: DoneFn) => {
    
    const mockResult: any = [
      {
          "desniv": "Basica",
          "codley": null,
          "codniv": "6"
      },
      {
          "desniv": "Universitario",
          "codley": null,
          "codniv": "4"
      },
      {
          "desniv": "Bachiller",
          "codley": null,
          "codniv": "5"
      },
      {
          "desniv": "Primaria",
          "codley": null,
          "codniv": "1"
      },
      {
          "desniv": "Secundaria",
          "codley": "SES",
          "codniv": "2"
      }
    ]
    // !! Not a good test, we can see the array is already greater than 1 in length
    expect(mockResult.length).toBeGreaterThan(1)

    getHttpClientSpy.get.and.returnValue(of(mockResult));

    procesosService.getAll()
      .subscribe((resp) => {
        expect(resp).toEqual(mockResult);
        // !! This map here is doing nothing.
        // !! Also, a map should be used for transformation and not assignment
        map((data: Procesos[]) => data = helpers.sortData(data, 'tippro'))       
        done();
      });
  }));

Here is how to test Http services that make HTTP requests: https://testing-angular.com/testing-services/#testing-a-service-that-sends-http-requests . I would use this HttpClientTestingModule because it should make the tests easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM