it can be more testable.
So, i implemented Test with mock features.
it using all network feature to use URLSession.
it support APITarget, so you can use attach parameter, Header easily more than URLSession
Like Alamofire.
- in Service class
import JasonBourne
final class NewsService {
private var bourne: Bourne!
func fetchNews() {
bourne = Bourne()
bourne.request(api: NewsAPI.topHeadlines) { result in
switch result {
case .success(let responseData):
print(String(data: responseData, encoding: .utf8))
case .failure(let error):
print(error)
}
}
}
func fetchFakeNews() {
bourne = Bourne(stubMode: .immediately)
bourne.request(api: NewsAPI.topHeadlines) { result in
switch result {
case .success(let responseData):
print(String(data: responseData, encoding: .utf8))
case .failure(let error):
print(error)
}
}
}
}
- If you want to StubMode,
Bourne(stubMode: .immediately)
in TargetAPI
import JasonBourne
public enum NewsAPI {
case topHeadlines
}
extension NewsAPI: APITarget {
public var baseURL: URL {
switch self {
case .topHeadlines:
return URL(string: "BaseURL")!
}
}
public var path: String {
switch self {
case .topHeadlines:
return "/top-headlines"
}
}
public var method: APIMethod {
switch self {
case .topHeadlines:
return .get
}
}
public var task: Parameter {
switch self {
case .topHeadlines:
let params: [String: String] = [
"country": "kr",
"apiKey": "private"
]
return .requestParameters(parameters: params)
}
}
public var headers: [String: String]? {
switch self {
case .topHeadlines:
return ["Content-Type": "application/json",
"Accept": "application/json"]
}
}
public var mockData: Data {
switch self {
case .topHeadlines:
return Data()
}
}
}
- the way is simillar way with Moya.
its done! Thank you.
- DownLoadTask - for DiskCaching
- async, await
i'm planing!
JasonBourne is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'JasonBourne'
rising-jun, [email protected]
JasonBourne is available under the MIT license. See the LICENSE file for more info.