-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test/#2 server chat module 테스트 코드 추가 #5
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
78a196d
🏗️ build(package에 jest-mock 추가): jest-mock 추가로 보다 쉬운 모킹 사용 가능
swkim12345 5c26a81
🧪test(chat.service.spec.ts): chat service 테스트 추가
swkim12345 b06fb06
🧪test(chat.service.spec.ts): chat service 테스트 추가
swkim12345 aaaebce
✨feat(server/packages, tsconfig): jest 절대경로 오류 해결
swkim12345 079a034
🧪test(server/chat.repository.spec.ts): repository 테스트 추가
swkim12345 b86e52e
🧪test(server/chat.gateway.spec.ts): toThrow사용해 error 체크, 테스트 구문 일관화
swkim12345 57ec421
🧪test(server/chat.gateway.spec.ts): gateway 테스트 추가
swkim12345 f37389c
🧪test(server/chat.module.spec.ts): 모듈 테스트 추가
swkim12345 9df25b7
🧪test(server/chat.repository.spec.ts): 테스트 부족한 부분 추가, describe 다른 곳과 통일화
swkim12345 eb1b716
🧪test(server/chat.service.spec.ts): chat.service existsRoom, existsPl…
swkim12345 fa53adc
📝docs(chat.service.spec.ts): todo 삭제
swkim12345 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,99 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { ChatGateway } from './chat.gateway'; | ||
import { ChatService } from './chat.service'; | ||
import { BadRequestException, PlayerNotFoundException, RoomNotFoundException } from 'src/exceptions/game.exception'; | ||
import { Socket } from 'socket.io'; | ||
|
||
describe('ChatGateway', () => { | ||
let gateway: ChatGateway; | ||
let mockSocket: Partial<Socket>; | ||
|
||
const mockChatService = { | ||
existsRoom: jest.fn(), | ||
existsPlayer: jest.fn(), | ||
sendMessage: jest.fn(), | ||
}; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ChatGateway], | ||
providers: [ChatGateway, { provide: ChatService, useValue: mockChatService }], | ||
}).compile(); | ||
|
||
gateway = module.get<ChatGateway>(ChatGateway); | ||
gateway = module.get(ChatGateway); | ||
|
||
/** | ||
* as unknown as 를 이용해 타입을 먼저 unknown으로 바꾼 다음, | ||
* 원하는 타입에 type assertion을 한다. | ||
*/ | ||
mockSocket = { | ||
handshake: { auth: { roomId: 'room1', playerId: 'player1' } }, | ||
data: {}, | ||
join: jest.fn(), | ||
to: jest.fn().mockReturnThis(), | ||
emit: jest.fn(), | ||
} as unknown as Socket; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('handleConnection 테스트', () => { | ||
it('roomId가 null일 때 BadRequestException을 발생', () => { | ||
mockSocket.handshake.auth = { roomId: null }; | ||
|
||
expect(() => gateway.handleConnection(mockSocket as Socket)).toThrow(BadRequestException); | ||
}); | ||
|
||
it('playerId가 null일 때 BadRequestException을 발생', () => { | ||
mockSocket.handshake.auth = { playerId: null }; | ||
|
||
expect(() => gateway.handleConnection(mockSocket as Socket)).toThrow(BadRequestException); | ||
}); | ||
|
||
it('room이 존재하지 않을 때 RoomNotFoundException을 발생', () => { | ||
mockChatService.existsRoom.mockReturnValue(false); | ||
|
||
expect(() => gateway.handleConnection(mockSocket as Socket)).toThrow(RoomNotFoundException); | ||
expect(mockChatService.existsRoom).toHaveBeenCalled(); | ||
}); | ||
|
||
it('플레이어가 룸에 존재하지 않을 때 PlayerNotFoundException을 발생', () => { | ||
mockChatService.existsRoom.mockReturnValue(true); | ||
mockChatService.existsPlayer.mockReturnValue(false); | ||
|
||
expect(() => gateway.handleConnection(mockSocket as Socket)).toThrow(PlayerNotFoundException); | ||
expect(mockChatService.existsPlayer).toHaveBeenCalled(); | ||
}); | ||
|
||
it('플레이어와 방이 정상적으로 할당되어 있을 때', () => { | ||
mockChatService.existsRoom.mockReturnValue(true); | ||
mockChatService.existsPlayer.mockReturnValue(true); | ||
|
||
gateway.handleConnection(mockSocket as Socket); | ||
|
||
expect(mockSocket.join).toHaveBeenCalled(); | ||
expect(mockSocket.data).toEqual({ roomId: 'room1', playerId: 'player1' }); | ||
}); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(gateway).toBeDefined(); | ||
describe('handleSendMessage 테스트', () => { | ||
it('데이터가 없을 때 BadRequestException을 발생', async () => { | ||
mockSocket.data = {}; | ||
|
||
await expect(gateway.handleSendMessage(mockSocket as Socket, { message: 'hello world' })).rejects.toThrow( | ||
BadRequestException, | ||
); | ||
}); | ||
|
||
it('정상적으로 메시지를 발신할 수 있을 때', async () => { | ||
mockSocket.data = { roomId: 'room1', playerId: 'player1' }; | ||
mockChatService.sendMessage.mockResolvedValue({ message: 'hello world', sender: 'player1' }); | ||
|
||
await gateway.handleSendMessage(mockSocket as Socket, { message: 'hello world' }); | ||
|
||
expect(mockChatService.sendMessage).toHaveBeenCalled(); | ||
expect(mockSocket.to('room1').emit).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저처럼 any를 쓰는 것보다는 괜찮아보이지만 이렇게 모든 mockSocket에 대해서 다시 as Socket을 붙여야 하니... 정말 뭔가뭔가... 아쉽네요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
맞아요.. 이거는 좀 아쉬운 거 같아요. 더 좋은 방법을 찾으면 업데이트할게요!