test case throwing error - TypeError: Cannot read properties of undefined (reading 'subscribe')

Discussão em 'Angular' iniciado por user3633028, Novembro 14, 2024.

  1. user3633028

    user3633028 Guest

    I am getting error "TypeError: Cannot read properties of undefined (reading 'subscribe')". Below is the report.service.ts code

    saveReport(reportData: any, reportType: string, reportId: string, reportExists: boolean): Observable<any> {
    const url =
    environment.META_API_ROOT +
    `/cat/report?reportType=` +
    reportType +
    (reportId ? `&reportId=${reportId}` : '') +
    `&reportExists=${reportExists}`;
    return this.http.post(url, reportData);
    }


    The test case for above service is as below which is throwing the error "TypeError: Cannot read properties of undefined (reading 'subscribe')"

    sample test case is below.

    it('should successfully call endpoint', (done) => {
    td.when(http.post(environment.API_ROOT + `/cat/report?reportType=reportType`, 'reportData')).thenReturn(
    of(Builder<Report>().calendarYears([2024]).build()),
    );

    service.saveReport('reportData', 'reportType', null, false).subscribe((result) => {
    expect(result).toEqual(Builder<Report>().calendarYears([2024).build());
    done();
    });
    });

    Continue reading...

Compartilhe esta Página