Created
November 27, 2016 20:04
-
-
Save thiagolioy/7632b185991bce54caad2a60f9e04f94 to your computer and use it in GitHub Desktop.
blog post tests, MockLoader
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
import Foundation | |
import ObjectMapper | |
@testable import Marvel | |
struct MockLoader { | |
let data: Data | |
let json: String | |
init?(file: String, withExtension fileExt: String = "json", in bundle:Bundle = Bundle.main) { | |
guard let path = bundle.path(forResource: file, ofType: fileExt) else { | |
return nil | |
} | |
let pathURL = URL(fileURLWithPath: path) | |
do { | |
data = try Data(contentsOf: pathURL, options: .dataReadingMapped) | |
if let decoded = NSString(data: data, encoding: 0) as? String { | |
json = decoded | |
} else { | |
return nil | |
} | |
} catch{ | |
return nil | |
} | |
} | |
} | |
extension MockLoader { | |
func map<T: Mappable>(to type: T.Type) -> T? { | |
return Mapper<T>().map(JSONString: json) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment