单元测试--异步

作者: 轻云绿原 | 来源:发表于2017-03-31 14:57 被阅读34次

用unit test测试异步线程时。有个问题,就是当主线程走完,测试就结束了,连同异步的计算或网络。不会等到异步出结果了,再来结束。所以要想办法让主线程阻塞,等下异步线程。

    func wait(){
        self.expectation(forNotification: "Test", object: nil, handler: nil)
        self.waitForExpectations(timeout: 10, handler: nil)
    }
    func notify(){
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "Test"), object: nil)
    }

应用

    func testExample() {
        let share = FlyNetWorking.share
        
        let filePath = "/Users/liumingqiu/Desktop/human.jpg"
        share.detect(face: filePath,progress:nil, success: { (data, _) in
            print("success")
            print(data)
            self.notify()
        }) { (errorStr) in
            print("errorStr:\(errorStr)\n")
            self.notify()
        }
        
        wait()
    }

相关文章

网友评论

    本文标题:单元测试--异步

    本文链接:https://www.haomeiwen.com/subject/srlvottx.html