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

RecipeList의 비즈니스 로직의 테스트를 작성했습니다. #23

Merged
merged 5 commits into from
Aug 19, 2024
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
122 changes: 84 additions & 38 deletions HomeCafeRecipes/HomeCafeRecipes.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

37 changes: 21 additions & 16 deletions HomeCafeRecipes/HomeCafeRecipes/Domain/Entities/Recipe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,27 @@ struct Recipe {

extension Recipe {

static func dummyRecipe() -> Recipe {
.init(
id: 1,
type: .coffee,
name: "",
description: "",
writer: .init(
id: 1,
profileImage: "",
nickname: "",
createdAt: Date()
),
imageUrls: [],
isLikedByCurrentUser: false,
likeCount: 0,
createdAt: Date()
static func dummyRecipe(
id: Int = 1,
type: RecipeType = .coffee,
name: String = "",
description: String = "",
writer: User = .init(id: 1, profileImage: "", nickname: "", createdAt: Date()),
imageUrls: [String] = [],
isLikedByCurrentUser: Bool = false,
likeCount: Int = 0,
createdAt: Date = Date()
) -> Recipe {
return Recipe(
id: id,
type: type,
name: name,
description: description,
writer: writer,
imageUrls: imageUrls,
isLikedByCurrentUser: isLikedByCurrentUser,
likeCount: likeCount,
createdAt: createdAt
)
}
}
115 changes: 115 additions & 0 deletions HomeCafeRecipes/HomeCafeRecipesTests/FetchFeedListUseCaseTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//
// FetchFeedListUseCaseTests.swift
// HomeCafeRecipesTests
//
// Created by 김건호 on 8/8/24.
//

import Foundation
import XCTest

import RxSwift

@testable
import HomeCafeRecipes

final class FetchFeedListUseCaseTests: XCTestCase {

var fetchRecipeListRepository: FeedListRepositoryMock!
var disposeBag: DisposeBag!

final class FeedListRepositoryMock: FeedListRepository {
var fetchRecipeListCallCount: Int = 0
var fetchRecipeListStub: Single<[Recipe]> = .just([Recipe.dummyRecipe()])

func fetchRecipes(pageNumber: Int) -> Single<[Recipe]> {
fetchRecipeListCallCount += 1
return fetchRecipeListStub
}
}

func createUseCase() -> FetchFeedListUseCase {
let usecase = FetchFeedListUseCaseImpl(repository: fetchRecipeListRepository)
return usecase
}

override func setUpWithError() throws {
fetchRecipeListRepository = .init()
disposeBag = .init()
}
}

extension FetchFeedListUseCaseTests {
func test_execute를_호출하면_FeedListRepository의_fetchRecipes을_호출합니다(){

// Given

let usecase = createUseCase()
fetchRecipeListRepository.fetchRecipeListStub = .just([Recipe.dummyRecipe()])

// When

usecase.execute(pageNumber: 0)
.subscribe()
.disposed(by: disposeBag)

// Then

XCTAssertEqual(fetchRecipeListRepository.fetchRecipeListCallCount, 1)
}

func test_FeedListRepository의_성공응답이오면_Recipe배열를_반환합니다(){
let usecase = createUseCase()
let recipe = [Recipe.dummyRecipe()]
fetchRecipeListRepository.fetchRecipeListStub = .just(recipe)
let expectation = self.expectation(description: "Fetch Recipes Success")

// 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)

wait(for: [expectation], timeout: 1.0)
XCTAssertEqual(fetchRecipeListRepository.fetchRecipeListCallCount, 1)
}

func test_FeedListRepository의_실패응답이오면_Error를_반환합니다() {
// Given
let usecase = createUseCase()
let error = NSError(domain: "TestError", code: -1)
fetchRecipeListRepository.fetchRecipeListStub = .error(error)
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)

wait(for: [expectation], timeout: 1.0)
XCTAssertEqual(fetchRecipeListRepository.fetchRecipeListCallCount, 1)
}
}
217 changes: 217 additions & 0 deletions HomeCafeRecipes/HomeCafeRecipesTests/RecipeListInteractorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
//
// RecipeListInteractorTests.swift
// HomeCafeRecipesTests
//
// Created by 김건호 on 8/5/24.
//

import Foundation
import XCTest

import RxSwift

@testable
import HomeCafeRecipes

final class RecipeListInteractorTests: XCTestCase {

var fetchFeedListUseCase: FetchFeedListUseCaseMock!
var searchFeedListUseCase: SearchFeedListUseCaseMock!
var delegate: RecipeListInteractorDelegateMock!
var disposeBag: DisposeBag!

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>> {
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>> {
executeCallCount += 1
return executeStub
}
}

final class RecipeListInteractorDelegateMock: RecipeListInteractorDelegate {
var fetchedCallCount: Int = 0
var showRecipeDetailCallCount: Int = 0
var fetchedRecipeResult: Result<[Recipe], Error>?
var receivedShowRecipeDetailID: Int?

func fetchedRecipes(result: Result<[Recipe], Error>) {
fetchedCallCount += 1
fetchedRecipeResult = result
}

func showRecipeDetail(ID: Int) {
showRecipeDetailCallCount += 1
receivedShowRecipeDetailID = ID
}
}

func createInteractor(pageNumber: Int = 0) -> RecipeListInteractor {
let interactor = RecipeListInteractorImpl(
fetchFeedListUseCase: fetchFeedListUseCase,
searchFeedListUseCase: searchFeedListUseCase
)
interactor.delegate = delegate
return interactor
}

override func setUpWithError() throws {
fetchFeedListUseCase = .init()
searchFeedListUseCase = .init()
delegate = .init()
disposeBag = .init()
}
}

extension RecipeListInteractorTests {

func test_화면이_로드될때_fetchedRecipes를_호출합니다(){

// Given
let interactor = createInteractor()

// When

interactor.viewDidLoad()

// Then
XCTAssertEqual(self.fetchFeedListUseCase.executeCallCount,1)
}

func test_FetchFeedListUseCase의_성공응답이오면_Delegate로_성공을_전달합니다() {

// Given
let interactor = createInteractor()
let recipes = [Recipe.dummyRecipe()]

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

interactor.viewDidLoad()

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

if case .success(let fetchedRecipe) = delegate.fetchedRecipeResult {
XCTAssertTrue(TestUtils.areRecipesEqual(fetchedRecipe,recipes))
} else {
XCTFail("Expected success but got failure or nil")
}
}

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

// Given
let interactor = createInteractor()
let error = NSError(domain: "TestError", code: -1)
fetchFeedListUseCase.executeStub = .just(.failure(error))

// When
interactor.viewDidLoad()

// Then
XCTAssertEqual(self.fetchFeedListUseCase.executeCallCount, 1)
XCTAssertEqual(self.delegate.fetchedCallCount, 1)

if case .failure(let receivedError as NSError) = delegate.fetchedRecipeResult {
XCTAssertEqual(receivedError.domain, error.domain)
XCTAssertEqual(receivedError.code, error.code)
} else {
XCTFail("Expected failure but got success or nil")
}
}

func test_searchRecipes_호출시_검색된_결과가_로드됩니다() {

// Given
let interactor = createInteractor()
let searchResultRecipes = [Recipe.dummyRecipe()]

searchFeedListUseCase.executeStub = .just(.success(searchResultRecipes))

// When
interactor.searchRecipes(with: "Test Query")

// Then
XCTAssertEqual(searchFeedListUseCase.executeCallCount, 1)
XCTAssertEqual(delegate.fetchedCallCount, 1)
if case .success(let fetchedRecipes) = delegate.fetchedRecipeResult {
XCTAssertTrue(TestUtils.areRecipesEqual(fetchedRecipes, searchResultRecipes))
} else {
XCTFail("Expected success but got failure or nil")
}
}

func test_fetchNextPage_호출시_다음_페이지_레시피가_로드됩니다() {

// Given
let interactor = createInteractor()
let initialRecipes = [Recipe.dummyRecipe()]
let nextPageRecipes = [Recipe.dummyRecipe(id: 2)]
fetchFeedListUseCase.executeStub = .just(.success(initialRecipes))
interactor.viewDidLoad()
fetchFeedListUseCase.executeStub = .just(.success(nextPageRecipes))

// When
interactor.fetchNextPage()

// Then
XCTAssertEqual(fetchFeedListUseCase.executeCallCount, 2)
XCTAssertEqual(delegate.fetchedCallCount, 2)
if case .success(let fetchedRecipes) = delegate.fetchedRecipeResult {
XCTAssertEqual(fetchedRecipes.count, 2)
XCTAssertTrue(TestUtils.areRecipesEqual(fetchedRecipes, initialRecipes + nextPageRecipes))
} else {
XCTFail("Expected success but got failure or nil")
}
}

func test_didSelectItem_호출시_레시피_상세화면으로_이동합니다() {

// Given
let interactor = createInteractor()
let selectedID = 1

// When
interactor.didSelectItem(ID: selectedID)

// Then
XCTAssertEqual(delegate.showRecipeDetailCallCount, 1)
XCTAssertEqual(delegate.receivedShowRecipeDetailID, selectedID)
}

func test_resetSearch_호출후_다시_searchRecipes_호출시_검색이_정상적으로_동작합니다() {

// Given
let interactor = createInteractor()
let initialRecipes = [Recipe.dummyRecipe()]
searchFeedListUseCase.executeStub = .just(.success(initialRecipes))

// When
interactor.searchRecipes(with: "Test Query")
interactor.resetSearch()

let newSearchRecipes = [Recipe.dummyRecipe(id: 2)]
searchFeedListUseCase.executeStub = .just(.success(newSearchRecipes))
interactor.searchRecipes(with: "New Query")

// Then
XCTAssertEqual(searchFeedListUseCase.executeCallCount, 2)
XCTAssertEqual(delegate.fetchedCallCount, 3)
if case .success(let fetchedRecipes) = delegate.fetchedRecipeResult {
XCTAssertTrue(TestUtils.areRecipesEqual(fetchedRecipes, newSearchRecipes))
} else {
XCTFail("Expected success but got failure or nil")
}
}
}
Loading
Loading