我正在尝试在我的 Nest.js 应用程序中测试服务。在我使用此服务的某些方法中,在某些情况下,我会根据上下文抛出新的 HttpException。
我正在尝试编写这些方法的单元测试,但我不知道如何测试抛出 HttpException 的情况。
我试过这个代码:
it('Shound return error for a non existing id provided', async () => {
await expect(service.getUser('false Id')).rejects.toThrow(new HttpException('This user does not exist', 404));
});
Run Code Online (Sandbox Code Playgroud)
但我收到了:
Expected the function to throw an error matching:
[Error: This user does not exist]
Instead, it threw:
Error: This user does not exist
Run Code Online (Sandbox Code Playgroud)
有没有人遇到过这个用例?
我正在尝试验证PUT users/端点的参数,但我希望所有参数都是可选的,但是一旦 api 使用者填写这些参数,我想验证它们。
我试图将@ApiModelProperty required false与验证管道装饰器结合起来,但验证管道接管了ApiModelProperty
(发送400 个错误请求 非常正常HttpException)
这是我的 DTO - usersUpdate.dto.ts:
import { IsEmail, IsEnum, IsPhoneNumber, IsEmpty } from 'class-validator';
import { ApiModelProperty } from '@nestjs/swagger';
import { RoleType } from './role-type.enum';
class UserInfo {
@ApiModelProperty({ description: 'User firstname', required: false })
readonly firstname: string;
@ApiModelProperty({ description: 'User lastname', required: false })
readonly lastname: string;
@ApiModelProperty({description: 'User postal address', required: false })
readonly address: string;
@ApiModelProperty({ description: 'User …Run Code Online (Sandbox Code Playgroud)