单元测试Unit Test

单元测试--异步

2017-03-31  本文已影响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()
    }
上一篇 下一篇

猜你喜欢

热点阅读