0

我正在做角度服务测试。它工作正常。但是我无法实现100%的测试,因为错误功能没有测试。我已经尽力了。但无法拿出 100%。有人帮我吗?

这是我的服务:

export class CandidateRegistrationManagementService {

    constructor(private http: HttpClient) { }

    getAvailabilityType(): Observable<ModelAvailabilityType> {
        return this.http.get<ModelAvailabilityType>(environment.setUpAndConfigUrl + `LookupTypeValue/LookupValueTypeDetails?LookupTypeName=AvailabilityType`)
            .pipe(
                map(events => {
                    return events;
                }),
                catchError(this.handleError)
            );
    }
    
    handleError(error: Response) {
        return throwError(error);
    }
}

我的测试用例目前我已经覆盖了 75%。错误测试的问题。这是我的尝试:

it('should throw error', () => {

        const httpError = {
            get: jest.fn(() => of(hot('#', {}, new Error('server error'))))
        };

        function handleError(error:any) {
            return throwError(error);
        }

        CRMService = new CandidateRegistrationManagementService(httpError as any);
        CRMService.getAvailabilityType().subscribe(data =>data, (error:any) => {
            catchError(handleError(error))
        })

    })

但我的尝试不起作用。我没有得到 100% 的覆盖率。请在这里寻求帮助。

4

1 回答 1

1

尝试使用 -- throwError rxjs 运算符,throwError 将执行代码的错误块。

   const httpError = {
            get: jest.fn(() => throwError(new Error('server error'))
        };
于 2020-07-03T15:29:57.103 回答