-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 유저 컨트롤러 테스트 코드 작성 - 유저 Dto 테스트 코드 작성 - 해시태그 컨트롤러 테스트 코드 수정 - 헬스체크 컨트롤러 테스트 코드 작성 Related to #29
- Loading branch information
Showing
4 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { HealthController } from './health.controller'; | ||
import { HealthCheckService, TypeOrmHealthIndicator } from '@nestjs/terminus'; | ||
import { HealthCheckResult } from '@nestjs/terminus/dist/health-check/health-check-result.interface'; | ||
import { ResponseEntity } from './common/entity/response.entity'; | ||
import { HttpStatus } from '@nestjs/common'; | ||
|
||
class MockHealthCheckService { | ||
check = jest.fn(); | ||
} | ||
|
||
class MockTypeOrmHealthIndicator { | ||
pingCheck = jest.fn(); | ||
} | ||
|
||
describe('HealthController', () => { | ||
let healthController: HealthController; | ||
let healthCheckService: HealthCheckService; | ||
let typeOrmHealthIndicator: TypeOrmHealthIndicator; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [HealthController], | ||
providers: [ | ||
{ provide: HealthCheckService, useClass: MockHealthCheckService }, | ||
{ | ||
provide: TypeOrmHealthIndicator, | ||
useClass: MockTypeOrmHealthIndicator, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
healthController = module.get<HealthController>(HealthController); | ||
healthCheckService = module.get<HealthCheckService>(HealthCheckService); | ||
typeOrmHealthIndicator = module.get<TypeOrmHealthIndicator>( | ||
TypeOrmHealthIndicator, | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(healthController).toBeDefined(); | ||
expect(healthCheckService).toBeDefined(); | ||
expect(typeOrmHealthIndicator).toBeDefined(); | ||
}); | ||
|
||
describe('readiness', () => { | ||
it('SUCCESS: 헬스체크 성공', async () => { | ||
const mockResult: HealthCheckResult = { | ||
status: 'ok', | ||
details: { database: { status: 'up' } }, | ||
info: {}, | ||
error: {}, | ||
}; | ||
|
||
jest.spyOn(healthCheckService, 'check').mockResolvedValue(mockResult); | ||
|
||
const response = await healthController.readiness(); | ||
|
||
expect(healthCheckService.check).toHaveBeenCalledWith([ | ||
expect.any(Function), | ||
]); | ||
expect(response.code).toEqual(HttpStatus.OK); | ||
expect(response).toEqual( | ||
ResponseEntity.OK_WITH<HealthCheckResult>( | ||
expect.any(String), | ||
mockResult, | ||
), | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { plainToInstance } from 'class-transformer'; | ||
import { GetUserList } from './get-user.dto'; | ||
import { User } from '../entity/user.entity'; | ||
|
||
describe('GetUserList', () => { | ||
let mockUserData: User; | ||
let getUserList: GetUserList; | ||
|
||
beforeEach(() => { | ||
mockUserData = plainToInstance(User, { | ||
id: 1, | ||
username: 'feed-me-admin1', | ||
email: '[email protected]', | ||
reviews: [], | ||
}); | ||
|
||
getUserList = new GetUserList(mockUserData); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(getUserList).toBeDefined(); | ||
expect(mockUserData).toBeInstanceOf(User); | ||
}); | ||
|
||
it('SUCCESS: User 엔티티로 GetUserList DTO 생성', () => { | ||
expect(getUserList.id).toEqual(mockUserData.id); | ||
expect(getUserList.username).toEqual(mockUserData.username); | ||
expect(getUserList.email).toEqual(mockUserData.email); | ||
expect(getUserList.review).toEqual(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { UserController } from './user.controller'; | ||
import { UserService } from './user.service'; | ||
import { User } from './entity/user.entity'; | ||
import { PaginationDto } from '../common/dto/get-pagination-query.dto'; | ||
import { PageEntity } from '../common/dto/get-pagination-list.dto'; | ||
import { GetUserList } from './dto/get-user.dto'; | ||
import { ResponseEntity } from '../common/entity/response.entity'; | ||
import { HttpStatus } from '@nestjs/common'; | ||
|
||
class MockUserService { | ||
findAllUser = jest.fn(); | ||
} | ||
|
||
describe('UserController', () => { | ||
let userController: UserController; | ||
let userService: UserService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [UserController], | ||
providers: [{ provide: UserService, useClass: MockUserService }], | ||
}).compile(); | ||
|
||
userController = module.get<UserController>(UserController); | ||
userService = module.get<UserService>(UserService); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(userController).toBeDefined(); | ||
expect(userService).toBeDefined(); | ||
}); | ||
|
||
describe('findAllUser', () => { | ||
it('SUCCESS: 유저 목록 반환', async () => { | ||
const mockRequest = new PaginationDto(); | ||
const mockResponse = [new User()]; | ||
const mockCount = 2; | ||
|
||
jest | ||
.spyOn(userService, 'findAllUser') | ||
.mockResolvedValue([mockResponse, mockCount]); | ||
|
||
const result = await userController.findAllUser(mockRequest); | ||
|
||
expect(userService.findAllUser).toHaveBeenCalledWith( | ||
mockRequest.getPageProps(), | ||
); | ||
expect(result.code).toEqual(HttpStatus.OK); | ||
expect(result).toEqual( | ||
ResponseEntity.OK_WITH<PageEntity<GetUserList>>( | ||
expect.any(String), | ||
expect.any(PageEntity<GetUserList>), | ||
), | ||
); | ||
}); | ||
}); | ||
}); |