如何在nestjs中使用函数装饰器测试控制器
我刚刚实现了一个在控制器中用作装饰器的拦截器,但是当我尝试为控制器运行测试时,拦截器似乎没有被调用,所以我不知道我是否做错了或者如果无法在单元测试中调用装饰器函数。
这是我的测试用例:
it('should throw an error', async () =>
const input = makeInput(1);
const user = makeUserWithApplications();
const postResponse = makeResponse(
'input', Status.USER_ERROR, HttpStatus.BAD_REQUEST
)
jest.spyOn(httpService, 'post').mockImplementation(() => {
throw new BadRequestException(postResponse);
});
jest.spyOn(repository, 'findById').mockImplementation(
() => Promise.resolve(input)
);
jest.spyOn(application, 'checkUserPermission').mockImplementation();
// jest.mock('../../../shared/exceptions/invalidSolverDataException.ts', () => ())
jest.spyOn(service, 'getData').mockImplementation(() => {
throw new BadRequestException(postResponse);
});
const exception = await getExceptionAsync(() =>
inputController.getData(1, user ))
expect(exception).toBeInstanceOf(InvalidSolverDataException);
});
这是控制器:
@UseInterceptors(SolverInterceptor)
@Get(':inputId/visuWithApi')
async getData(@Param('inputId') inputId: number, @GetUser() user: User): Promise<model> {
const input = await this.inputService.findById(
inputId, user.company.id, u => u.instance, u => u.instance.application
);
const application: Application = input.instance.application;
await this.applicationService.checkUserPermission(user, application);
return this.inputService.getData(input);
}
I have just implemented an interceptor that is used as a decorator in the controller, but when I try to run the test for the controller, the interceptor doesn't seem to be called, so I don't know if I am doing it wrong or if it is not possible to have the decorator functions called in unit tests.
here is my test case :
it('should throw an error', async () =>
const input = makeInput(1);
const user = makeUserWithApplications();
const postResponse = makeResponse(
'input', Status.USER_ERROR, HttpStatus.BAD_REQUEST
)
jest.spyOn(httpService, 'post').mockImplementation(() => {
throw new BadRequestException(postResponse);
});
jest.spyOn(repository, 'findById').mockImplementation(
() => Promise.resolve(input)
);
jest.spyOn(application, 'checkUserPermission').mockImplementation();
// jest.mock('../../../shared/exceptions/invalidSolverDataException.ts', () => ())
jest.spyOn(service, 'getData').mockImplementation(() => {
throw new BadRequestException(postResponse);
});
const exception = await getExceptionAsync(() =>
inputController.getData(1, user ))
expect(exception).toBeInstanceOf(InvalidSolverDataException);
});
and this the controller:
@UseInterceptors(SolverInterceptor)
@Get(':inputId/visuWithApi')
async getData(@Param('inputId') inputId: number, @GetUser() user: User): Promise<model> {
const input = await this.inputService.findById(
inputId, user.company.id, u => u.instance, u => u.instance.application
);
const application: Application = input.instance.application;
await this.applicationService.checkUserPermission(user, application);
return this.inputService.getData(input);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论