ios - How do I fix a testing issue in Swift when I forget to set a delegate? -


background: i'm learning swift , tdd simultaneously self improvement purposes , try keep current tech. issue, i'm not calling bug, made me scratch head while. centers around xml parsing operation passing test when shouldn't because delegate not set, , not work.

here classes of interest:

class parseoperation: nsoperation, nsxmlparserdelegate {     private var parser: nsxmlparser?     var datatoparse: nsdata?      private var parseresults: anyobject?     private var parseerror: nserror?      override func main() {         if let data = datatoparse {             parser = nsxmlparser(data: data)              // neglecting set delegate of parser self not cause test fail             // parser?.delegate = self              parser?.parse()         }     }      func getparseresults() -> anyobject? { return parseresults }     func getparseerror() -> nserror? { return parseerror }      func parser(parser: nsxmlparser, didendelement elementname: string, namespaceuri: string?, qualifiedname qname: string?) {         // parsing stuff         parseresults = nsobject()     }      func parser(parser: nsxmlparser, parseerroroccurred parseerror: nserror) {         self.parseerror = parseerror     } } 

here have minimal parsing setup xml. neglecting set parser's delegate none of nsxmlparsedelegate called.

class manager: nsobject {    func parsesomexmlinaqueue(completion:(results: anyobject?, error: nserror?) -> ()) {        let operation: parseoperation = {            var tempparseop = parseoperation()            tempparseop.datatoparse = "<?xml version=\"1.0\"?><a/>".datausingencoding(nsutf8stringencoding)            tempparseop.completionblock = {                completion(results: tempparseop.getparseresults(), error: tempparseop.getparseerror())            }            tempparseop.qualityofservice = .userinitiated            tempparseop.queuepriority = .normal             return tempparseop        }()         let queue = nsoperationqueue()        queue.addoperation(operation)    } } 

a manager class creates parse operation , tosses queue run on thread.

class test: xctestcase {    func testparserparsesgoodxmldata() {        let expectation = expectationwithdescription("parser should execute closure")         let manager = manager()        manager.parsesomexmlinaqueue { results, error in            // so, block called, fulfill expection don't timeout            expectation?.fulfill()             if let someerror = error {                xctfail("got error")            }             // test our parsing successful            xctassertnotnil(results, "should object parser")        }         waitforexpectationswithtimeout(1, handler: nil)    } } 

and, test should fail due results object being nil, succeeds reason.

setting breakpoint on xctassertnotnil causes sorts of interesting results. first, continuing after breakpoint hit gives sigabrt signal. then, continuing again steps our thread , gives test pass. gives?


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -