Skip to content
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

[Feature25] 유닛 테스트 추가 #26

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

yujung5873
Copy link
Collaborator

#️⃣연관된 이슈

#25

📝작업 내용

Given - When - Then 패턴을 사용하여

  1. mainviewmodel에서 searchPhotos
  • 성공시 _photos LiveData가 업데이트 되는지 검증
  • 실패시 _apiError LiveData가 업데이트 되는지 검증
  1. unsplash API를 요청한 뒤 적절한 데이터를 반환하는지 검증
스크린샷 2025-02-08 오후 7 46 14 스크린샷 2025-02-08 오후 7 46 32 스크린샷 2025-02-08 오후 7 46 57

@yujung5873 yujung5873 requested a review from f-lab-tony February 8, 2025 10:47
@yujung5873 yujung5873 self-assigned this Feb 8, 2025
),
total = 10000,
total_pages = 1000
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

지금 작성해주신대로 ApiService를 Mock객체로 사용하여 Repository의 로직을 테스트하는 패턴은 많이 사용하긴 합니다.
하지만 이 패턴은 네트워크호출을 직접 수행하지 않기때문에, 별도로 추가한 interceptor들은 테스트를 할 수가 없는 단점이 있습니다.
interceptor들은 어떻게 테스트할 수 있을까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

interceptor는 MockWebServer를 사용하는 방법이 있습니다. MockWebServer는 테스트 환경에서 서버를 모킹하고, 원하는 응답을 반환시킬 수 있습니다. 이를 통해 실제 서버 없이 테스트할 수 있고, Intercepteor가 요청을 잘 변경하는지 확인할 수 있습니다.


coVerify { unsplashRepository.searchPhotos(query) }

viewModel.apiError.removeObserver(observer)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Given-When-Then 패턴을 사용하셨으니, 주석으로 영역을 구분해주시면 코드를 읽을때 가독성이 좋아집니다~
혹은, println()으로 영역을 구분하면서,

  • 어떤 준비과정이 있고(Given)
  • 어떤 동작을 수행할 때(When)
  • 어떤 결과가 있는지(Then)

써두면, 이 코드가 어떤 테스트인지를 좀 더 명확하게 파악하는데 도움이 됩니다!
요건 이번 PR에서 개선해주세요~

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

넵 알겠습니다!


viewModel.searchPhotos(query)

advanceUntilIdle()
Copy link
Collaborator

Choose a reason for hiding this comment

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

이건 어떤 역할을 하는 메서드인가요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

advanceUntilIdle()는 테스트 환경에서 코루틴의 실행을 끝까지 진행시키는 역할을 합니다.

테스트 코드에서 코루틴을 사용하면 코루틴이 비동기적으로 실행이 되어 즉시 완료되지 않을 수 있는데, advanceUntilIdle()을 호출하면 현재 실행 중이거나 대기 중인 모든 코루틴이 완료될 때까지 실행을 진행해줍니다.

Copy link

sonarqubecloud bot commented Feb 8, 2025

Quality Gate Failed Quality Gate failed

Failed conditions
31.0% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

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