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

267
Views
How to create a test for the below controller?

do I need to write a test for the below code?

@Post()
    update(@Body() updateUserDto: UpdateUserDto, @Request() req) {
        return this.userService.update(req.user.user, updateUserDto);
      }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I'm going to make some assumptions about injected properties of the controller class. For full setups, you can view my testing example repo here. This should be essentially what you're looking for though

describe('UserController (Unit)', () => {
  let controller: UserController;
  let service: MockedClass<UserService>;

  beforeAll(async () => {
    const modRef = await Test.createTestingModule({
      controllers: [UserController],
      providers: [
        {
          provide: UserService,
          useValue: {
            update: jest.fn().mockReturnValue({ hello: 'world' }),
          },
        }
      ],
    }).compile();
    controller = modRef.get(UserController);
    service = modRef.get(UserService);
  });
  it('should call update and return the value', () => {
    const bodyVal = objectThatMatchesUpdateUserDto;
    const reqVal = {
      user: {
        user: valueThatMatchesReqUserUser
      }
    };
    const res = controller.update(bodyVal, reqVal);
    expect(res).toEqual({ hello: 'world' });
    expect(service.update).toBeCalledWith(reqVal.user.user, bodyVal)
  });
});
about 4 years ago · Juan Pablo Isaza 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!