Complete the code to import the testing module for a NestJS controller test.
import { Test, TestingModule } from '@nestjs/[1]';
In NestJS, the testing utilities are imported from '@nestjs/testing'. This module helps create a testing environment for controllers and services.
Complete the code to create a testing module for the controller named 'CatsController'.
const moduleRef: TestingModule = await Test.createTestingModule({ controllers: [[1]] }).compile();When testing a controller, you include it in the 'controllers' array of the testing module. Here, we want to test 'CatsController'.
Fix the error in retrieving the controller instance from the testing module.
const controller = moduleRef.[1](CatsController);To get an instance of a provider or controller from the testing module, use the 'get' method.
Fill both blanks to mock a service method and test the controller's response.
jest.spyOn(service, '[1]').mockImplementation(() => [2]);
We spy on the 'findAll' method of the service and mock it to return a list of cats. This helps test the controller without calling the real service.
Fill all three blanks to write a test that checks if the controller's getAll method returns the mocked data.
it('should return an array of cats', async () => { const result = await controller.[1](); expect(result).toEqual([2]); expect(service.[3]).toHaveBeenCalled(); });
The test calls the controller's 'getAll' method, expects the mocked array, and checks that the service's 'findAll' method was called.