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

FetchRecipeDeatil의 비즈니스 로직을 테스트 해보았습니다. #16

Merged
merged 11 commits into from
Jul 23, 2024

Conversation

GeonH0
Copy link
Collaborator

@GeonH0 GeonH0 commented Jul 21, 2024

FetchRecipeDetailUseCaseTests를 생성하였습니다

  • FetchRecipeRepository의 목객체를 생성해서 Usecase를 테스트 했습니다.
  • 비 동기 처리를 위해 expectation을 활용하여 1초의 딜레이를 주었습니다.
  • 총 3가지 테스트를 진행했습니다.
  1. execute를 호출하면 1번 RecipeDetailRepository의 fetchRecipeDetail을 호출합니다
  2. RecipeDetailRepository의 성공응답이오면 Recipe를 반환합니다
  3. RecipeDetailRepository의 실패응답이오면 Error를 반환합니다

RecipeDetailInteractorTests를 생성하였습니다

  • FetchRecipeDetailUseCase,RecipeDetailInteractorDelegate 2개의 목객체를 생성해서 테스트를 진행했습니다.
  • 총 3가지 테스트를 진행했습니다.
  1. 화면이 로드될때 FetchRecipeDetailUsecase를 실행하는지
  2. FetchRecipeDetailUsecase의 성공응답이오면 Delegate로 성공을 전달하는지
  3. FetchRecipeDetailUsecase의 실패응답이오면 Delegate로 실패를 전달하는지
  • Recipe에 dummy 데이터를 추가하는 메서드를 추가하였습니다.

extension Recipe {

static func dummy() -> Recipe {
.init(id: 1, type: .coffee, name: "", description: "", writer: .init(id: 1, profileImage: "", nickname: "", createdAt: Date()), imageUrls: [], isLikedByCurrentUser: false, likeCount: 0, createdAt: Date())

Choose a reason for hiding this comment

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

라인수가 길어지는 경우, 줄바꿈 해주세요.
또 이런 init 단계에 파라미터를 넣어주는 코드 같은 경우에는 언제든지 확장되어 길어질 수 있으니 줄바꿈을 하면서 작성하는게 좋아요. 앞으로 줄바꿈 같은 코드 스타일 코멘트는 줄여나가도록 더 신경써주시면 좋을 것 같아요.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

[d7ab88c] 수정했습니다

Comment on lines 20 to 34
class FetchRecipeDetailUseCaseMock: FetchRecipeDetailUseCase {
var executeCallCount: Int = 0
var executeStub: Single<Result<Recipe, Error>> = .just(.failure(NSError()))
func execute(recipeID: Int) -> Single<Result<Recipe, Error>> {
executeCallCount += 1
return executeStub
}
}

class RecipeDetailInteractorDelegateMock: RecipeDetailInteractorDelegate {
var fetchedCallCount: Int = 0
var fetchedRecipeResult: Result<Recipe, Error>?
func fetchedRecipe(result: Result<Recipe, Error>) {
fetchedCallCount += 1
fetchedRecipeResult = result

Choose a reason for hiding this comment

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

얘네는 final class로 선언하시지 않은 이유가 있나요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

final 처리가 아직 익숙하지 않아서 놓친거 같습니다
좀 더 신경 쓰도록 하겠습니다
[415e9a4] 수정했습니다.

Comment on lines 53 to 56
override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
}

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.

[18959c6] 제거했습니다.


extension RecipeDetailInteractorTests {

func test_화면이_로드될때_FetchRecipeDetailUsecase를_실행합니다() {

Choose a reason for hiding this comment

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

FetchRecipeDetailUseCase를 실행하는게 아니라 FetchRecipeDetailUseCase의 execute를 호출하는거 아닐까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

[0cc06c1] 네이밍 수정했습니다

func test_FetchRecipeDetailUsecase의_성공응답이오면_Delegate로_성공을_전달합니다() {
// given
let interactor = createInteractor()
let recipe = Recipe.dummy()

Choose a reason for hiding this comment

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

dummyRecipe로 네이밍하는게 더 명확하지 않을까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

[80597bd] 수정했습니다

XCTAssertEqual(self.fetchRecipeDetailUsecase.executeCallCount, 1)
XCTAssertEqual(self.delegate.fetchedCallCount, 1)

if case .success(let fetchedRecipe)? = self.delegate.fetchedRecipeResult {

Choose a reason for hiding this comment

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

여기에는 self 필요 없을 것 같은데 확인부탁드려요~

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

[7e579b8] 수정했습니다

XCTAssertEqual(self.fetchRecipeDetailUsecase.executeCallCount, 1)
XCTAssertEqual(self.delegate.fetchedCallCount, 1)

if case .failure(let fetchedError as NSError) = self.delegate.fetchedRecipeResult {

Choose a reason for hiding this comment

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

여기에는 self 필요 없을 것 같은데 확인부탁드려요~

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

[7e579b8] 수정했습니다

var fetchRecipeDetailRepository: FetchRecipeRepositoryMock!
var disposeBag: DisposeBag!

class FetchRecipeRepositoryMock: RecipeDetailRepository {

Choose a reason for hiding this comment

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

final class 고민 부탁해요

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

[415e9a4] 수정했습니다

}

func createUseCase() -> FetchRecipeDetailUseCase {
let usecase = FetchRecipeDetailUseCaseImpl(repository: fetchRecipeDetailRepository)

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.

[d0d27b0] 수정했습니다

Comment on lines 40 to 41
override func tearDownWithError() throws {
}

Choose a reason for hiding this comment

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

안쓰면 제거해주세요

Copy link
Collaborator Author

@GeonH0 GeonH0 Jul 22, 2024

Choose a reason for hiding this comment

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

[18959c6] 제거 했습니다


extension FetchRecipeDetailUseCaseTests {

func test_execute를_호출하면_1번_RecipeDetailRepository의_fetchRecipeDetail을_호출합니다(){

Choose a reason for hiding this comment

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

이 테스트 코드에서 1번 호출하는건 검증하고 있지 않은 것 같네요?
여러번 호출해도 1번만 calleCount가 올라가는지 검증해야 테스트 케이스와 맞겠네요.

Copy link
Collaborator Author

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.

[61854b3] 수정했습니다

Comment on lines +106 to +112
.subscribe(onSuccess: { result in
if case .failure(let receivedError as NSError) = result {
// Then

XCTAssertEqual(receivedError.domain, error.domain)
XCTAssertEqual(receivedError.code, error.code)
expectation.fulfill()

Choose a reason for hiding this comment

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

질문) onSuccess 내부에서 failure 검증이 성공했나요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

스크린샷 2024-07-22 오전 12 21 00
이런식으로 테스트 결과가 나왔는데 검증이 성공된거 아닌가요?

Choose a reason for hiding this comment

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

execute에서 .success로 failure를 전달하나보네요

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

넵 맞습니다

Copy link

@GeonH0 GeonH0 merged commit a9dc689 into feature/FixInputOutput Jul 23, 2024
2 checks passed
@GeonH0 GeonH0 deleted the feature/RecipeDetailTest branch September 12, 2024 01:19
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