Skip to content

Instantly share code, notes, and snippets.

@emndeniz
Created September 19, 2022 13:46
Show Gist options
  • Save emndeniz/0408888c369b69cfdc9d0ff21583993b to your computer and use it in GitHub Desktop.
Save emndeniz/0408888c369b69cfdc9d0ff21583993b to your computer and use it in GitHub Desktop.
JSONTestHelper
import Foundation
/// This is a helper class for Unit Tests to load required response
class JSONTestHelper {
/// Reads local json file from test resources
/// - Parameter name: File name without extension
/// - Returns: Data represantation of file
func readLocalFile(name: String) -> Data? {
do {
let bundle = Bundle(for: type(of: self))
if let filePath = bundle.path(forResource: name, ofType: "json"){
let jsonData = try String(contentsOfFile: filePath).data(using: .utf8)
return jsonData
}
} catch {
fatalError("Failed to get json")
}
return nil
}
/// Decodes given jsonData to desired object
/// - Parameters:
/// - decodeType: Generic Decodable type
/// - jsonData: JSON Data
/// - Returns: Generic Decodable Type
func decode<T>(decodeType:T.Type, jsonData:Data) -> T where T:Decodable {
let decoder = JSONDecoder()
do {
let response = try decoder.decode(T.self, from: jsonData)
return response
} catch {
fatalError("Failed to get decodable type")
}
}
/// Reads json file and converts it to desired object
/// - Parameters:
/// - decodeType: Generic Decodable type
/// - name: File name without extension
/// - Returns: Generic Decodable Type
func readAndDecodeFile<T>(decodeType:T.Type, name: String) -> T where T:Decodable {
guard let data = readLocalFile(name: name) else {
fatalError("Data is nil")
}
return decode(decodeType: decodeType, jsonData: data)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment