我正在编写一个测试用例,以测试jest中的handleRequestsFailed和isObjectUndefinedOrEmpty。但是由于handleRequestsFailed取决于isObjectUndefinedOrEmpty的返回值,因此出现错误:抛出新的InternalServerErrorException( | ^ 8 | “获取内容的请求失败!” 9 | ); 。以下是我在spec和ts文件中的内容。
// ts file
export function handleRequestsFailed(res: any, 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;
}
// spec file
import { handleRequestsFailed } from './shared-methods';
describe('Schedule Assistant', () => {
beforeEach(async () => {
});
it('should return an error if isObjectUndefinedOrEmpty returns an undefined or empty object', () => {
const res={}
const assistant = new handleRequestsFailed(res);
expect(assistant).toEqual({});
});
});