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

리팩토링으로 인한 수정된 내용 테스트코드에 반영 #40

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion HomeCafeRecipes/HomeCafeRecipes/Domain/Entities/User.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

struct User {
struct User: Equatable {
let id: Int
let profileImage: String
let nickname: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ final class FetchFeedListUseCaseTests: XCTestCase {
var fetchRecipeListCallCount: Int = 0
var fetchRecipeListStub: Single<[Recipe]> = .just([Recipe.dummyRecipe()])

func fetchRecipes(pageNumber: Int) -> Single<[Recipe]> {
func fetchRecipes(
currentPage: Int,
targetPage: Int,
boundaryID: Int
) -> Single<[Recipe]> {
fetchRecipeListCallCount += 1
return fetchRecipeListStub
}
Expand All @@ -49,9 +53,13 @@ extension FetchFeedListUseCaseTests {

// When

usecase.execute(pageNumber: 0)
.subscribe()
.disposed(by: disposeBag)
usecase.execute(
currentPage: 0,
targetPage: 0,
boundaryID: 0
)
.subscribe()
.disposed(by: disposeBag)

// Then

Expand All @@ -66,20 +74,24 @@ extension FetchFeedListUseCaseTests {

// When

usecase.execute(pageNumber: 0)
.subscribe(onSuccess:{ result in
if case .success(let fetchedRecipes) = result {
XCTAssertTrue(TestUtils.areRecipesEqual(fetchedRecipes, recipe))
expectation.fulfill()
} else {
XCTFail("Expected success but got failure")
}

}, onFailure: { error in
XCTFail("Expected success but got error: \(error)")

})
.disposed(by: disposeBag)
usecase.execute(
currentPage: 0,
targetPage: 0,
boundaryID: 0
)
.subscribe(onSuccess:{ result in
if case .success(let fetchedRecipes) = result {
XCTAssertTrue(TestUtils.areRecipesEqual(fetchedRecipes, recipe))
expectation.fulfill()
} else {
XCTFail("Expected success but got failure")
}

}, onFailure: { error in
XCTFail("Expected success but got error: \(error)")

})
.disposed(by: disposeBag)

wait(for: [expectation], timeout: 1.0)
XCTAssertEqual(fetchRecipeListRepository.fetchRecipeListCallCount, 1)
Expand All @@ -93,21 +105,25 @@ extension FetchFeedListUseCaseTests {
let expectation = self.expectation(description: "Fetch Recipes Failure")

// When
usecase.execute(pageNumber: 0)
.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()
} else {
XCTFail("Expected failure but got success")
}
}, onFailure: { error in
XCTFail("Expected failure but got error: \(error)")
})
.disposed(by: disposeBag)
usecase.execute(
currentPage: 0,
targetPage: 0,
boundaryID: 0
)
.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()
} else {
XCTFail("Expected failure but got success")
}
}, onFailure: { error in
XCTFail("Expected failure but got error: \(error)")
})
.disposed(by: disposeBag)

wait(for: [expectation], timeout: 1.0)
XCTAssertEqual(fetchRecipeListRepository.fetchRecipeListCallCount, 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,43 @@ final class RecipeListInteractorTests: XCTestCase {

final class FetchFeedListUseCaseMock: FetchFeedListUseCase {
var executeCallCount: Int = 0
var executeStub: Single<Result<[Recipe], Error>> = .just(.failure(NSError(domain: "Test", code: -1, userInfo: nil)))
func execute(pageNumber: Int) -> Single<Result<[Recipe], Error>> {
var executeStub: Single<Result<[Recipe], Error>> = .just(
.failure(
NSError(
domain: "Test",
code: -1,
userInfo: nil
)
)
)
func execute(
currentPage: Int,
targetPage: Int,
boundaryID: Int
) -> Single<Result<[Recipe], Error>> {
executeCallCount += 1
return executeStub
}
}

final class SearchFeedListUseCaseMock: SearchFeedListUseCase {
var executeCallCount: Int = 0
var executeStub: Single<Result<[Recipe], Error>> = .just(.failure(NSError(domain: "Test", code: -1, userInfo: nil)))

func execute(title: String, pageNumber: Int) -> Single<Result<[Recipe], Error>> {
var executeStub: Single<Result<[Recipe], Error>> = .just(
.failure(
NSError(
domain: "Test",
code: -1,
userInfo: nil
)
)
)

func execute(
title: String,
currentPage: Int,
targetPage: Int,
boundaryID: Int
) -> Single<Result<[Recipe], Error>> {
executeCallCount += 1
return executeStub
}
Expand All @@ -49,7 +74,7 @@ final class RecipeListInteractorTests: XCTestCase {
fetchedCallCount += 1
fetchedRecipeResult = result
}

func showRecipeDetail(ID: Int) {
showRecipeDetailCallCount += 1
receivedShowRecipeDetailID = ID
Expand Down Expand Up @@ -109,6 +134,26 @@ extension RecipeListInteractorTests {
}
}

func test_boundaryID가_최대값으로_설정됩니다() {
// Given
let interactor = createInteractor()
let recipes = [
Recipe.dummyRecipe(id: 1),
Recipe.dummyRecipe(id: 3),
Recipe.dummyRecipe(id: 2)
]

fetchFeedListUseCase.executeStub = .just(.success(recipes))

// When
interactor.viewDidLoad()

// Then
XCTAssertEqual(fetchFeedListUseCase.executeCallCount, 1)
XCTAssertEqual(delegate.fetchedCallCount, 1)
XCTAssertEqual(recipes.map( {$0.id} ).max(), 3)
}

func test_FetchFeedListUseCase의_실패응답이오면_Delegate로_실패를_전달합니다() {

// Given
Expand Down Expand Up @@ -157,7 +202,7 @@ extension RecipeListInteractorTests {
// Given
let interactor = createInteractor()
let initialRecipes = [Recipe.dummyRecipe()]
let nextPageRecipes = [Recipe.dummyRecipe(id: 2)]
let nextPageRecipes = [Recipe.dummyRecipe(id: 2)]
fetchFeedListUseCase.executeStub = .just(.success(initialRecipes))
interactor.viewDidLoad()
fetchFeedListUseCase.executeStub = .just(.success(nextPageRecipes))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ final class SearchFeedListUseCaseTests: XCTestCase {
var searchRecipeListStub: Single<[Recipe]> = .just([Recipe.dummyRecipe()])
func searchRecipes(
title: String,
pageNumber: Int
currentPage: Int,
targetPage: Int,
boundaryID: Int
) -> Single<[Recipe]>
{
searchRecipeListCallCount += 1
Expand Down Expand Up @@ -50,7 +52,9 @@ final class SearchFeedListUseCaseTests: XCTestCase {

usecase.execute(
title: "",
pageNumber: 0
currentPage: 0,
targetPage: 0,
boundaryID: 0
)
.subscribe()
.disposed(by: disposeBag)
Expand All @@ -70,7 +74,9 @@ final class SearchFeedListUseCaseTests: XCTestCase {
// When
usecase.execute(
title: "Test Recipe",
pageNumber: 1
currentPage: 0,
targetPage: 0,
boundaryID: 0
)
.subscribe(onSuccess: { result in
switch result {
Expand Down Expand Up @@ -98,20 +104,24 @@ final class SearchFeedListUseCaseTests: XCTestCase {
let expectation = self.expectation(description: "Search Recipes Failure")

// When
usecase.execute(title: "Test Recipe", pageNumber: 1)
.subscribe(onSuccess: { result in
switch result {
case .success:
XCTFail("Expected failure but got success")
case .failure(let receivedError as NSError):
XCTAssertEqual(receivedError.domain, error.domain)
XCTAssertEqual(receivedError.code, error.code)
expectation.fulfill()
}
}, onFailure: { error in
XCTFail("Expected failure but got error: \(error)")
})
.disposed(by: disposeBag)
usecase.execute(title: "Test Recipe",
currentPage: 0,
targetPage: 0,
boundaryID: 0
)
.subscribe(onSuccess: { result in
switch result {
case .success:
XCTFail("Expected failure but got success")
case .failure(let receivedError as NSError):
XCTAssertEqual(receivedError.domain, error.domain)
XCTAssertEqual(receivedError.code, error.code)
expectation.fulfill()
}
}, onFailure: { error in
XCTFail("Expected failure but got error: \(error)")
})
.disposed(by: disposeBag)

wait(for: [expectation], timeout: 1.0)
}
Expand Down
Loading