iOS SwiftUI Factory with test
사용 언어: Swift 5 사용 버전: Xcode 15.2 iOS 스위프트유아이 Factory with test를 알아보겠습니다. Factory는 DI(Dependency Injection, 의존성 주입)를 위한 라이브러리입니다. 그리고 Resolver라는 DI library를 대체하는 오픈 소스입니다. 공식 문서입니다. https://hmlongco.github.io/Factory/documentation/factory 참고 프로젝트: https://github.com/Jaehwa-Noh/Practice-Amphibians/tree/swiftui-factory-amphibians-app iOS SwiftUI Factory 게시글과 이어집니다. https://shwoghk14.blogspot.com/2024/01/ios-swiftui-factory.html Unit test부터 한 번 봅시다. import Factory를 해줍니다. AmphibiansViewModelTest.swift setUpWithError에 Container.shared.reset()을 해줍니다. 그리고 기존의 코드는 놔두고 그 밑에 Container.shared.networkAmphibiansInfoRepository.register를 적어줍니다. register는 우리가 이전 시간에 작성한 Container extension을 register에 적힌 함수로 대체하겠다는 뜻입니다. 즉, 가장 아래의 Container.shared.amphibiansViewModel()은 fakeAmphibiansInfoRepository가 사용되게 됩니다. 실행해 봅시다. 문제없이 잘 테스트가 됩니다. 다른 파일들도 이렇게 register를 사용해서 갈아주면 됩니다. NetworkAmphibiansRepositoryTest.swift AmphibiansRemoteDataSourceTest.swift UI test를 볼까요? 원래 앱의 Factory를 사용하기 때문에 따로 코드를 변경해 줄 필요가 없습니다...