【发布时间】:2021-01-04 13:24:25
【问题描述】:
您好,我想要 http 帖子的单元测试覆盖 catchError 分支:
getSession() {
const options = {
headers: {
'Content-Type': 'application/json',
'Accept-API-Version': 'resource=2.0'
}
};
return this.http.post(SESSION_REST_API_URL, '', options as any)
.pipe(
timeoutWith(10000, throwError(new Error('exceeded'))),
catchError((error: any) => throwError(error))
);
}
在我的测试“它”中,我尝试收听 toHaveReturnedWith 和 toEqual 订阅此帖子但未涵盖:
it('should throwError', () => {
// arrange
const error = throwError({error: 400});
const options = {headers: {'Content-Type': 'application/json',, 'Accept-API-Version': 'resource=2.0'}}
;
UtilsTest.mockReturnValue(service['http'], 'post', throwError(error));
// act
service.getSession();
// assert 1
expect(service['http'].post(SESSION_REST_API_URL, '', options)).toHaveReturnedWith(of({error: 400}));
// assert 2
expect(loginCredentialsService.getSessionJsonResponse()).toHaveReturnedWith(error)
// assert 3
service['http'].post(SESSION_REST_API_URL, '', options).subscribe({
error: (err) => {
expect(err).toEqual({error: 400});
}
});
});
【问题讨论】:
标签: angular unit-testing http ts-jest