forked from codesquad-members-2021/sidedish
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test: MockNetwork클래스와 UnitTest작성 (#23)
- Loading branch information
1 parent
962f4a5
commit 0d849bf
Showing
9 changed files
with
314 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
SideDishApp/SideDishApp/Data/Network/MockNetworkManager.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// MockNetworkManager.swift | ||
// SideDishApp | ||
// | ||
// Created by 이다훈 on 2021/04/27. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
|
||
protocol URLSessionProtocol { | ||
func dataTaskPublisher(for url: URL) -> URLSession.DataTaskPublisher | ||
} | ||
|
||
extension URLSession : URLSessionProtocol{ | ||
} | ||
|
||
struct MockNetworkSessionSuccess : URLSessionProtocol { | ||
func dataTaskPublisher(for url: URL) -> URLSession.DataTaskPublisher { | ||
|
||
return URLSession.shared.dataTaskPublisher(for: Bundle.main.url(forResource: "testData", withExtension: "json")!) | ||
} | ||
} | ||
|
||
class MockNetworkManager : NetworkManager { | ||
override func get<T>(url: URL?) -> AnyPublisher<T, NetworkError> where T : Decodable { | ||
|
||
guard let myUrl = url else { | ||
return Fail(error: NetworkError.urlError).eraseToAnyPublisher() | ||
} | ||
|
||
return session.dataTaskPublisher(for: myUrl) | ||
.mapError { _ in NetworkError.networkConnection } | ||
.flatMap { data, response -> AnyPublisher<T, NetworkError> in | ||
return Just(data) | ||
.decode(type: T.self, decoder: JSONDecoder()) | ||
.mapError { error in | ||
print(error.localizedDescription) | ||
return NetworkError.parsing }.eraseToAnyPublisher() | ||
}.eraseToAnyPublisher() | ||
} | ||
|
||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.