hoang-tran/TestNetworkLayer

Problem migrating to Swift 3

Opened this issue · 1 comments

Hey Hoang,

I tried to rebuild your awesome tutorial in Xcode 8.2.1 / Swift 3. I'm stuck with the XCTest extension:

public func stub(urlString: String, jsonFileName: String) -> Mockingjay.Stub {
let path = NSBundle(forClass: self.dynamicType).pathForResource(jsonFileName, ofType: "json")!
let data = NSData(contentsOfFile: path)!
return stub(uri(urlString), builder: jsonData(data))
}

public func stub(urlString: String, error: NSError) -> Mockingjay.Stub {
return stub(uri(urlString), builder: failure(error))
}

Any idea how to rewrite the data as Data since contentsOfFile is no longer an option?

I re-wrote it like this and it works for me:

public func stub(_ urlString: String, jsonFileName: String) -> Mockingjay.Stub {
    let path = Bundle(for: type(of: self)).path(forResource: jsonFileName, ofType: "json")!
    let data = try? Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
    return stub(uri(urlString), jsonData(data!))
}

public func stub(_ urlString: String, error: NSError) -> Mockingjay.Stub {
    return stub(uri(urlString), failure(error))
}