Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
Prueba de unidad de método que falla en nestjs

Tengo el siguiente método en mi controlador.

 @Controller() export class AppController { @Get('whoami') @Roles(Role.All) @ApiResponse({ status: 200, type: User }) currentUser(@Req() request: Request) { return request.user; } }

Estoy escribiendo debajo del caso de prueba para el mismo.

 describe('AppController', () => { let controller: AppController; beforeAll(async () => { const app: TestingModule = await Test.createTestingModule({ controllers: [AppController], }).compile(); controller = app.get<AppController>(AppController); }); it('should be defined', () => { expect(controller).toBeDefined(); }); it('currentUser should be called', () => { expect(controller.currentUser).toBeDefined(); expect(controller.currentUser).toEqual("shruti"); }); });

no solo esto está fallando, sino que también return request.user; no estar cubierto por la cobertura. abajo está el error.

ingrese la descripción de la imagen aquí

que mal estoy haciendo

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

controller.currentUser es una función. Toma el objeto Request y devuelve request.user . Su prueba debe verse algo como

 it('currentUser should be called', () => { expect(controller.currentUser).toBeDefined(); expect(controller.currentUser({ user: "shruti" } as any)).toEqual("shruti"); });

El as any es necesario aquí porque Request tiene muchas más propiedades que solo user , pero todo lo que le importa a su prueba y código es que user está allí para ser devuelto.

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!