samedi 5 décembre 2015

NSURLSessionDataTask is not resumed in Unit Test

I have included

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>

in the Info.plist of the application AND into the Info.plist of the tests app.

If a run the app everything works correctly but If I call the following in a unit test class the following method does not work anymore:

    func entrypoint(completion: (jsonData:[NSDictionary]) -> Void) {
        let url = NSURL(string: Constants.EntryPointLink)
        let task = self.session.dataTaskWithURL(url!) { (data, response, error) -> Void in
            // The following code is never run in unit test
            guard error == nil && data != nil else {
                // Handle error
                print(error?.localizedDescription)
                return
            }
            do {
                if case let dictionaries as [NSDictionary] = try NSJSONSerialization.JSONObjectWithData(data!, options: []) {
                    completion(jsonData: dictionaries)
                }
            } catch let er as NSError {
                print("Fail to parse: \(er)")
            }
        }
        task.resume()
    }

I have debug the method, and the block of code inside dataTaskWithURL is never executed even if the task is resumed. I need some other settings?

Aucun commentaire:

Enregistrer un commentaire