我正在编写一个测试用例,当提供的响应未定义时应该抛出错误。但是我得到这个错误
if (isObjectUndefinedOrEmpty(res) && isObjectUndefinedOrEmpty(res2)) {
> 5 | throw new InternalServerErrorException(
| ^
6 | 'Requests to get content failed!'
7 | );
8 | }
PFB代码和我的规格文件。
// spec file
it('should return an error if isObjectUndefinedOrEmpty returns an undefined or empty object', () => {
const res=
undefined
expect( ()=> {
handleRequestsFailed(res)
}).toThrow(new InternalServerErrorException('Requests to get content failed!'));
});
// ts file
export function handleRequestsFailed(res, res2?: any) {
if (isObjectUndefinedOrEmpty(res) && isObjectUndefinedOrEmpty(res2)) {
throw new InternalServerErrorException(
'Requests to get content failed!'
);
}
}
export function isObjectUndefinedOrEmpty(obj: any) {
return !obj || Object.keys(obj).length === 0;
}