如何在角度7中测试此Observable函数

时间:2019-11-25 23:45:35

标签: angular testing

我有此功能,我需要测试,我不知道该怎么办!

public flightStream(prefix: AvaliableHttpPrefix, url: string, headers: any, body?: any): Observable<FlightData[] | FlightData> {
    return timer(GENERAL_CONSTANTS.FLIGHTS_INITIAL_DELAY, GENERAL_CONSTANTS.FLIGHTS_REFRESH_TIME)
      .pipe(
        flatMap(_ => {
          return this.httpHandlerService.request(prefix, url, headers, body)
        }),
        map(response => {
          if (Array.isArray(response)) {
            console.log(response);
            return response as FlightData[]
          }
          return response as FlightData
        })
      )
  }

我尝试做这个

it('should call get function and return a list of flights', () => {
    const flightsService: FlightsService = TestBed.get(FlightsService);
    const stream$ = flightsService.flightStream(flightListStreamMock.prefix, flightListStreamMock.URL, flightListStreamMock.headers, flightListStreamMock.filterCriteria);
    stream$.subscribe((result) => {
      expect(result).toBe(FLIGHTS)
    })
  })

但是它说Timeout - Async callback was not invoked within 5000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL)

1 个答案:

答案 0 :(得分:0)

您需要研究使用fakeAsync。 您的服务正在使用计时器,fakeAsync将允许您使用tick()来模拟测试的时间流逝。