单元测试--异步

用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()
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容