Skip to content

test/pet-unit#54

Merged
emibgo2 merged 26 commits intodevfrom
test/pet-unit
Feb 20, 2025
Merged

test/pet-unit#54
emibgo2 merged 26 commits intodevfrom
test/pet-unit

Conversation

@hyoseonlim
Copy link
Copy Markdown
Member

@hyoseonlim hyoseonlim commented Dec 26, 2024

🛠️ 코드 리팩토링

pet 관련 테스트 코드를 작성했습니다.

변경 사항

새로 작성한 Mock Repository 클래스 및 테스트 파일 외의 주요 수정 사항은 아래와 같습니다.

  1. 테스트 실행 시 'cannot find module ~' 에러가 발생하여, 상대 경로로 import하는 방식으로 해결했습니다.
    참고) https://stackoverflow.com/questions/53963007/error-while-running-nestjs-in-production-mode-cannot-find-module
  • src/auth/presentation/user.dto.ts
  • src/pet/presentation/pet.controller.ts
  1. DTO - Domain - Entity 간의 변환 과정에서 특정 클래스에만 특정 필드가 누락되어 발생하는 BadRequestException 와 같은 오류를 해결했습니다.

테스트 방법

yarn test test/unit/pet/application/pet.service.test.ts
yarn test test/unit/pet/presentation/pet.controller.test.ts
yarn test test/unit/pet/pet.domain.test.ts

참고 사항

  1. IntelliJ의 organize imports 기능을 적용했더니 거의 모든 파일이 커밋되어버려 (SHA: 264100e) revert로 되돌리기를 했습니다..! 확인 부탁드립니다 ㅜㅜ

  2. jest.fn(), jest.spyOn() 등을 활용하는 게 좋을지 몰라서 우선 보류하고 Customer 파트 테스트코드 형식을 참고해서 작성했습니다.

@hyoseonlim hyoseonlim changed the title Test/pet unit test/pet-unit Jan 16, 2025
@hyoseonlim hyoseonlim marked this pull request as ready for review January 23, 2025 11:59
@hyoseonlim hyoseonlim requested a review from emibgo2 January 23, 2025 12:00
});

async findOne(id: number, customer: ICustomer): Promise<PetEntity> {
const pet = await this.petRepository.getOne(id);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

service에서는 findOne이고 repository에서는 getOne인데 두개 동일하게 맞춰주세요!
get~은 존재하지않으면 exception이고 find는 그렇지 않습니다.

Comment thread src/pet/port/pet.repository.ts Outdated
});
}

findOneById(petId: number): Promise<PetEntity> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getOneById라는 이름이 더 적합할거 같아요!

@CurrentCustomer() customer: CustomerEntity,
): Promise<PetChecklistDto[]> {
return await this.petService.findCheckList(category, type, null, customer);
return await this.petService.findCheckList(category, type, null);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이부분도 get과 find가 혼용되어서 사용되고 있습니다

Comment thread src/pet/presentation/pet.controller.ts Outdated
@ApiOkResponse({ type: PetDto, description: '반려동물 정보 조회 성공' })
@Get('/my')
async getAll(@CurrentCustomer() customer: CustomerEntity): Promise<Pet[]> {
async getAll(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기도!

@@ -93,7 +106,7 @@ export class PetController {
async getOne(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기도!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좀더 다양한 테스트 케이스를 작성해보는건 어떨까요??
예를들어 ChecklistType에따라서 petChecklistChoices나 petChecklistAnswer 둘중 null로 반환되는 경우 등이 있을것 같아요

@emibgo2 emibgo2 self-requested a review February 18, 2025 12:48
Copy link
Copy Markdown
Contributor

@emibgo2 emibgo2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

잘하신것 같아요!
마음은 Approve 이지만 컨플릭을 해결해주세요 :(
그리고 추가적으로 테스트 코드 작성할때 it 말고 test로 테스트 코드를 표현하는게 좋아보여요!
it은 영어로 작성할때는 코드가 더 잘 읽히지만 한글인 경우엔
test로 하는게 더 직관적인것 같아서 다른 test 코드들이 test 함수로 테스트 코드를 작성했습니다.

두가지 부분만 해결해주시고 알려 주시면 바로 approve 하겠습니다!

expect(pet.customer.customerName).toBe(customer.customerName);
expect(pet.customer.authProvider).toBe(customer.authProvider);
});
it('PetDto의 breedId가 존재하지 않을 때 BadRequestException을 발생시킨다.', async () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요런 케이스 테스트 하는거 좋은것 같아요!

expect(pet.petName).toBe(createdPet.petName);
});

it('특정 customer의 pet이 아닐 경우 ForbiddenException를 발생시킨다', async () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

expect(checklist).toBeDefined();
expect(Array.isArray(checklist)).toBe(true);
});
it('체크리스트 타입이 Choice일 때 petChecklistAnswer는 null이다.', async () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍👍👍👍

@emibgo2 emibgo2 enabled auto-merge February 20, 2025 03:25
@emibgo2 emibgo2 self-requested a review February 20, 2025 22:49
Copy link
Copy Markdown
Contributor

@emibgo2 emibgo2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍👍👍👍

@emibgo2 emibgo2 merged commit 068198c into dev Feb 20, 2025
@emibgo2 emibgo2 deleted the test/pet-unit branch February 20, 2025 22:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants