简体   繁体   English

单元测试 Angular Http 请求 pipe

[英]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我必须对这个 observable 执行单元测试,其中发现 map 将数据转换为所需的

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我已经完成了以下测试它但是跳过了 map 之后的调用

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 .以下是如何测试发出 HTTP 请求的 Http 服务: 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.我会使用这个HttpClientTestingModule因为它应该使测试更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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