Skip to content

Instantly share code, notes, and snippets.

@thiagolioy
Created November 27, 2016 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiagolioy/7632b185991bce54caad2a60f9e04f94 to your computer and use it in GitHub Desktop.
Save thiagolioy/7632b185991bce54caad2a60f9e04f94 to your computer and use it in GitHub Desktop.
blog post tests, MockLoader
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