Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { UsersService } from './users.service';
- import { UsersController } from './users.controller';
- import { Test, TestingModule } from '@nestjs/testing';
- import { PassportModule } from '@nestjs/passport';
- describe('UsersController', () => {
- let controller: UsersController;
- let usersService: any;
- beforeEach(async () => {
- usersService = {
- getAllUsers() {/* empty */ },
- getUserById() { /* empty */ },
- };
- const module: TestingModule = await Test.createTestingModule({
- controllers: [UsersController],
- providers: [
- {
- provide: UsersService,
- useValue: usersService,
- },
- ],
- imports: [
- PassportModule.register({ defaultStrategy: 'jwt' }),
- ],
- }).compile();
- controller = module.get<UsersController>(UsersController);
- });
- it('should be defined', () => {
- expect(controller).toBeDefined();
- });
- describe('getAllUsers()', () => {
- it('should call usersService.getAllUsers()', async () => {
- const spy = jest.spyOn(usersService, 'getAllUsers');
- await controller.getAllUsers({});
- expect(usersService.getAllUsers).toHaveBeenCalledTimes(1);
- spy.mockClear();
- });
- it('should return the result from usersService.getAllUsers()', async () => {
- const fakeUsers = [{ username: 'test1' }, { username: 'test2' }];
- const expectedResult = [{ username: 'test1' }, { username: 'test2' }];
- const spy = jest
- .spyOn(usersService, 'getAllUsers')
- .mockReturnValue(Promise.resolve(fakeUsers));
- const result = await controller.getAllUsers({});
- expect(result).toEqual(expectedResult);
- spy.mockClear();
- });
- });
- describe('getUserById()', () => {
- it('should call usersService.getUserById()', async () => {
- const fakeId = 1;
- const spy = jest.spyOn(usersService, 'getUserById');
- await controller.getUserById(fakeId);
- expect(usersService.getUserById).toHaveBeenCalledTimes(1);
- spy.mockClear();
- });
- it('should return the result from usersService.getUserById()', async () => {
- const fakeId = 1;
- const fakeUser = { id: 1, username: 'test' };
- const expectedResult = { id: 1, username: 'test' };
- const spy = jest
- .spyOn(usersService, 'getUserById')
- .mockReturnValue(Promise.resolve(fakeUser));
- const result = await controller.getUserById(fakeId);
- expect(result).toEqual(expectedResult);
- spy.mockClear();
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement