【问题标题】:What is the difference in semaphore vs the Expectation in iOS Unit Test for asynchronous api layer testing信号量与iOS单元测试中异步api层测试的期望有什么区别
【发布时间】:2018-02-11 08:52:28
【问题描述】:

我已经使用 semaphore 和 expect 实现了,结果是一样的。两者的根本区别是什么。

// 使用期望

func testDownloadWithExpectation(){

        let expect = expectation(description: "Download should exceed")

        if let url = URL(string: "https://httpbin.org"){
            Downloader.download(from: url, completionHandler: { (data, response, error) in
                XCTAssertNil(error, "\(String(describing: error?.localizedDescription)) error occured")
                XCTAssertNotNil(data,"No Payload returned")
                expect.fulfill()
            })
        }

        waitForExpectations(timeout: 10) { (error) in
            XCTAssertNil(error, "Test timed Out")
        }
    }

// 使用信号量

    func testDownloadWIthSephaMore(){

        let sepahamore = DispatchSemaphore(value: 0)
        if let url = URL(string: "https://httpbin.org"){
            Downloader.download(from: url, completionHandler: { (data, response, error) in
                XCTAssertNil(error, "\(String(describing: error?.localizedDescription)) error occured")
                XCTAssertNotNil(data,"No Payload returned")
                sepahamore.signal()
            })
        }
        let timeout = DispatchTime.now() + DispatchTimeInterval.seconds(5)
        if sepahamore.wait(timeout: timeout) == DispatchTimeoutResult.timedOut {
            XCTFail("Test timed out")
        }
    }

【问题讨论】:

    标签: semaphore xctest xctestcase xctestexpectation


    【解决方案1】:

    我相信期望会等待期望被实现,并会在等待的同时继续运行其他测试。

    我认为信号量会在等待信号量标志时阻止其他测试运行

    【讨论】:

      猜你喜欢
      • 2014-01-30
      • 2011-02-14
      • 1970-01-01
      • 2021-11-01
      • 2013-03-14
      • 2011-07-18
      • 1970-01-01
      • 1970-01-01
      • 2020-08-27
      相关资源
      最近更新 更多