【发布时间】:2021-11-02 13:58:28
【问题描述】:
我正在尝试为一个简单的功能做这个测试,但我做不到。我发布了我的测试代码和错误。
我尝试了几种不同的方法,但都没有成功。 我正在使用 NestJS CLI 和使用 jestJs 的测试
// My coding
createSession(login: string, password: string) {
const search: UserEntity = this.users.find(
(user: UserEntity) => user.cpf === login,
);
if (search) return search.senha === password;
else throw new HttpException('UNAUTHORIZED', HttpStatus.UNAUTHORIZED);
}
// My test
it('should retrieve getHello', async () => {
await expect(
service.createSession(mockLogin.login, mockLogin.password),
).rejects.toEqual(
new HttpException('UNAUTHORIZED', HttpStatus.UNAUTHORIZED),
);
});
// The error
● LoginService › Get service › should retrieve getHello
HttpException: UNAUTHORIZED
22 | );
23 | if (search) return search.senha === password;
> 24 | else throw new HttpException('UNAUTHORIZED', HttpStatus.UNAUTHORIZED);
| ^
25 | }
26 | }
27 |
【问题讨论】:
-
请不要使用代码图像,而是使用降价格式的代码块。代码图像不可分割,使问题更难阅读和诊断
-
做一些调试。例如:将
console.log(this.users)添加到您的createSession方法中 -
代码没问题,我有用户反馈,我的问题是要模拟HttpException。