typescript - hvordan teste controller med funksjonsdekoratører i nestjs
Jeg har nettopp implementert en interceptor som brukes som en dekoratør i kontrolleren, men når jeg prøver å kjøre testen for kontrolleren, ser det ut til at interceptoren ikke blir kalt, så jeg vet ikke om jeg gjør det feil eller om det ikke er mulig å få dekoratørfunksjonene til å bli kalt i enhetstester.
her er mitt testtilfelle :
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);
});
og dette er kontrolleren:
@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);
}