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

HomeViewModel 변경에 따른 테스트 코드 수정 #92

Merged
merged 3 commits into from
Nov 28, 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct StubMemorialHouseRepository: MemorialHouseRepository {
self.dummyData = dummyData
}

func fetchMemorialHouse() async -> MemorialHouse {
return dummyData
func fetchMemorialHouse() async -> Result<MemorialHouse, any Error> {
return .success(dummyData)
}
}
16 changes: 6 additions & 10 deletions MemorialHouse/MHDomain/MHDomainTests/UserHouseUseCaseTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import Testing
struct MemorialHouseUseCaseTest {
var sut: FetchMemorialHouseUseCase!

@Test mutating func test유저하우스_엔티티모델_가져오기() async {
@Test mutating func test유저하우스_엔티티모델_가져오기() async throws {
// Arrange 준비 단계: 테스트 대상 시스템(SUT)와 의존성을 원하는 상태로 만들기
let dummyMemorialHouse = MemorialHouse(
name: "더미데이터",
categories: ["가족", "친구"],
bookCovers: [
BookCover(title: "책1", imageURL: "Temp", color: .beige, category: "가족"),
BookCover(title: "책2", imageURL: "Temp", color: .beige, category: "친구")
Expand All @@ -18,43 +17,40 @@ struct MemorialHouseUseCaseTest {
self.sut = DefaultFetchMemorialHouseUseCase(repository: stubMemorialHouseRepository)

// Act 실행 단계: SUT 메소드를 호출하면서 의존성을 전달해서 결과를 저장하기
let result = await sut.execute()
let result = try await sut.execute()

// Assert 검증 단계: 결과와 기대치를 비교해서 검증하기
#expect(result.name == dummyMemorialHouse.name)
#expect(result.categories == dummyMemorialHouse.categories)
#expect(result.bookCovers == dummyMemorialHouse.bookCovers)
}

@Test mutating func test세글자_이상인_경우_원본_문자열_그대로_반환() async {
@Test mutating func test세글자_이상인_경우_원본_문자열_그대로_반환() async throws {
// Arrange 준비 단계: 테스트 대상 시스템(SUT)와 의존성을 원하는 상태로 만들기
let dummyMemorialHouse = MemorialHouse(
name: "Hello",
categories: [],
bookCovers: []
)
let stubMemorialHouseRepository = StubMemorialHouseRepository(dummyData: dummyMemorialHouse)
self.sut = DefaultFetchMemorialHouseUseCase(repository: stubMemorialHouseRepository)

// Act 실행 단계: SUT 메소드를 호출하면서 의존성을 전달해서 결과를 저장하기
let result = await sut.execute()
let result = try await sut.execute()

// Assert 검증 단계: 결과와 기대치를 비교해서 검증하기
#expect(result.name == "Hello")
}

@Test mutating func test두글자_이하인_경우_글자_사이에_공백추가() async {
@Test mutating func test두글자_이하인_경우_글자_사이에_공백추가() async throws {
// Arrange 준비 단계: 테스트 대상 시스템(SUT)와 의존성을 원하는 상태로 만들기
let dummyMemorialHouse = MemorialHouse(
name: "Hi",
categories: [],
bookCovers: []
)
let stubMemorialHouseRepository = StubMemorialHouseRepository(dummyData: dummyMemorialHouse)
self.sut = DefaultFetchMemorialHouseUseCase(repository: stubMemorialHouseRepository)

// Act 실행 단계: SUT 메소드를 호출하면서 의존성을 전달해서 결과를 저장하기
let result = await sut.execute()
let result = try await sut.execute()

// Assert 검증 단계: 결과와 기대치를 비교해서 검증하기
#expect(result.name == "H i")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,13 @@ public final class HomeViewModel: ViewModelType {

private let output = PassthroughSubject<Output, Never>()
private let fetchMemorialHouseUseCase: FetchMemorialHouseUseCase
private let fetchCategoryUseCase: FetchCategoriesUseCase
private var cancellables = Set<AnyCancellable>()
private(set) var houseName = ""
private(set) var bookCovers = [BookCover]()
private(set) var currentBookCovers = [BookCover]()

public init(
fetchMemorialHouseUseCase: FetchMemorialHouseUseCase,
fetchCategoryUseCase: FetchCategoriesUseCase
) {
public init(fetchMemorialHouseUseCase: FetchMemorialHouseUseCase) {
self.fetchMemorialHouseUseCase = fetchMemorialHouseUseCase
self.fetchCategoryUseCase = fetchCategoryUseCase
}

@MainActor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ public struct HomeViewModelFactory {
}

public func make() -> HomeViewModel {
HomeViewModel(
fetchMemorialHouseUseCase: fetchMemorialHouseUseCase,
fetchCategoryUseCase: fetchCategoryUseCase
)
HomeViewModel(fetchMemorialHouseUseCase: fetchMemorialHouseUseCase)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ struct HomeViewModelTest {
// Arrange 준비 단계: 테스트 대상 시스템(SUT)와 의존성을 원하는 상태로 만들기
let dummyMemorialHouse = MemorialHouse(
name: "효준",
categories: ["가족", "친구"],
bookCovers: [
BookCover(title: "책1", imageURL: "Temp", color: .beige, category: "가족"),
BookCover(title: "책2", imageURL: "Temp", color: .beige, category: "친구")
Expand All @@ -36,7 +35,6 @@ struct HomeViewModelTest {

// Assert 검증 단계: 결과와 기대치를 비교해서 검증하기
#expect(sut.houseName == "효준")
#expect(sut.categories == ["전체", "즐겨찾기", "가족", "친구"])
#expect(sut.bookCovers.count == 2)
}

Expand All @@ -45,7 +43,6 @@ struct HomeViewModelTest {
// Arrange 준비 단계: 테스트 대상 시스템(SUT)와 의존성을 원하는 상태로 만들기
let dummyMemorialHouse = MemorialHouse(
name: "효준",
categories: ["가족", "친구"],
bookCovers: [
BookCover(title: "책1", imageURL: "Temp", color: .beige, category: "가족"),
BookCover(title: "책2", imageURL: "Temp", color: .beige, category: "친구")
Expand All @@ -67,46 +64,11 @@ struct HomeViewModelTest {
try await Task.sleep(nanoseconds: 500_000_000)

// Act 실행 단계: SUT 메소드를 호출하면서 의존성을 전달해서 결과를 저장하기
input.send(.selectedCategory(index: 3)) // 전체, 즐겨찾기, 가족, 친구 중에서 친구 선택
input.send(.selectedCategory(category: "친구")) // 전체, 즐겨찾기, 가족, 친구 중에서 친구 선택
try await Task.sleep(nanoseconds: 500_000_000)

// Assert 검증 단계: 결과와 기대치를 비교해서 검증하기
#expect(sut.currentBookCovers.count == 1)
#expect(sut.currentBookCovers.first?.title == "책2")
}

@MainActor
@Test mutating func test유효하지_않은_인덱스를_선택하면_에러를_발생시킨다() async throws {
// Arrange 준비 단계: 테스트 대상 시스템(SUT)와 의존성을 원하는 상태로 만들기
let dummyMemorialHouse = MemorialHouse(
name: "효준",
categories: ["가족", "친구"],
bookCovers: [
BookCover(title: "책1", imageURL: "Temp", color: .beige, category: "가족"),
BookCover(title: "책2", imageURL: "Temp", color: .beige, category: "친구")
]
)
let stubFetchMemorialHouseUseCase = StubFetchMemorialHouseUseCase(dummyMemorialHouse: dummyMemorialHouse)
self.sut = HomeViewModel(fetchMemorialHouseUseCase: stubFetchMemorialHouseUseCase)

let input = PassthroughSubject<HomeViewModel.Input, Never>()
var receivedOutputs: [HomeViewModel.Output] = []

sut.transform(input: input.eraseToAnyPublisher())
.sink { output in
receivedOutputs.append(output)
}
.store(in: &cancellables)

input.send(.viewDidLoad)
try await Task.sleep(nanoseconds: 500_000_000)

// Act 실행 단계: SUT 메소드를 호출하면서 의존성을 전달해서 결과를 저장하기
receivedOutputs.removeAll()
input.send(.selectedCategory(index: 999))
try await Task.sleep(nanoseconds: 500_000_000)

// Assert 검증 단계: 결과와 기대치를 비교해서 검증하기
#expect(receivedOutputs.isEmpty)
}
}
Loading