func with completion

Hi

 

I’m unit testing some functions with completion handlers but I’m unable to get the callbacks to fire   Here is the code being tested:

class MyClass1 {
    static func updateName(with myStruct: inout, completion: @escaping (Error?) -> Void) {
        var newName: String?
        var updateError: Error?
       
        MyClass2.createNewName(myStruct.name) {
            (name, error) in
            newName = name
            updateError = error
        }
       
        /
        if let newName = newName {
            myStruct.name = newName
        }
        competion(updateError)
    }
}
   
   
class MyClass2 {
    static func createNewName(_ oldName: String, @escaping (String?, Error?) -> Void) {
        /
        let task = URLSession.shared.dataTask(with: URL(string: "https:/
            (data, response, error) in
           
            /
           
            guard error == nil else {
                return completion(nil, error!)
            }
           
           
            let result = String(data: data!, .utf8)
            completion(result, error)
        }
        task.resume()
    }
}
   
struct MyStruct {
    private var _name: String
   
    var Name: String {
        get {
            return _name
        }
        set {
            _name = newValue
        }
    }
}

 

 

When I execute MyClass2.createNewName directly, the completion block executes and the function suceeds fine.

 

However, when I call MyClass1.updateName which calls into MyClass2.createNewName the completion block in MyClass2.createNewName doesn’t get executed and the myStruct.name = newName is never hit.

 

What could I be doing wrong?  And any suggestions to resolve this

 

Thanks

Powered by WPeMatico

About

You may also like...

Comments are closed.