如何在nestjs中使用函数装饰器测试控制器

发布于 2025-01-10 13:54:37 字数 1642 浏览 0 评论 0原文

我刚刚实现了一个在控制器中用作装饰器的拦截器,但是当我尝试为控制器运行测试时,拦截器似乎没有被调用,所以我不知道我是否做错了或者如果无法在单元测试中调用装饰器函数。

这是我的测试用例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文