Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在XCTESCASE的setUp()中等待多个异步调用_Ios_Swift_Asynchronous_Xctestcase - Fatal编程技术网

Ios 在XCTESCASE的setUp()中等待多个异步调用

Ios 在XCTESCASE的setUp()中等待多个异步调用,ios,swift,asynchronous,xctestcase,Ios,Swift,Asynchronous,Xctestcase,在运行test()方法之前,我需要执行多个调用。我有一个完成块,我正在使用waitforexpections()。因为有不止一个异步调用,所以我使用了一个计数器。我让expection.fulfill()只在计数器达到调用数时发生 override func setUp() { super.setUp() let exp = expectation(description: "waitForSetUp") var counter = 0 // Issue an

在运行
test()
方法之前,我需要执行多个调用。我有一个完成块,我正在使用
waitforexpections()
。因为有不止一个异步调用,所以我使用了一个计数器。我让
expection.fulfill()
只在计数器达到调用数时发生

override func setUp() {
    super.setUp()

    let exp = expectation(description: "waitForSetUp")
    var counter = 0

    // Issue an async request
    self.addEventToCalendar(title: "Test1", description: "Description test1", startDate: NSDate().addingTimeInterval(-36000), endDate: NSDate()){(success, error) in
        if (success) && (error == nil) {
            counter = counter + 1
            if(counter == 2){exp.fulfill()}
        }
    }

    self.addEventToCalendar(title: "Test2", description: "Description test2", startDate: NSDate(), endDate: NSDate().addingTimeInterval(36000)){(success, error) in
        if (success) && (error == nil) {
            counter = counter + 1
            if(counter == 2){exp.fulfill()}
        }
    }

    waitForExpectations(timeout: 40, handler: nil)        
}
这种结构不起作用。
test()
方法有时在调用返回之前运行(不总是)


如何使
setUp()
等待返回多个异步调用?

我也遇到过类似的情况。我最终要做的解决方案是调用几个函数,这些函数为我的先决条件添加期望值,并将期望值的超时设置为合理的值。在期望的完成处理程序中,我触发了设置算法的下一步。在所有初步步骤通过后,我开始实际的测试逻辑

附加到苹果文档

编辑:请参见下面的示例代码:

class CommonTests: XCTestCase {
    var validate: XCTestExpectation? = nil

    func testMytest() {        
      validate(completion: {
        loadSomeStuff(completion: { (list: [Stuff]?) in
          // actual test
        }
      })
    }

    func validate(completion: @escaping ()->()) {
        self.validateExpectation = self.expectation(description: "Setup")
        // async operation can be fired here
        // or if already started from somewhere else just wait for it to complete


        self.waitForExpectations(timeout: 60) { (error: Error?) in
            XCTAssert((error == nil), error?.localizedDescription ?? "Failed with unknown error")
            completion()
        }
    }

    func validateAsyncCompleted() {
      self.validateExpectation?.fulfill()
    }

    func loadStuff(completion: @escaping ([Stuff]?)->()) {

      // possible place for assertion or some other checks

      let expectation = self.expectation(description: "loading")
      DispatchQueue.global().async {

        let result: [Stuff]? = nil
        // load operation

        expectation.fulfill()
        completion(result)
      }

      self.waitForExpectations(timeout: 90) {  (error: Error?) in
        XCTAssert((error == nil), error?.localizedDescription ?? "load - failed with unknown error")
      }
    }
}

注意:对于期望有两种方法,第一种方法保存在变量中,因此如果需要,可以从另一个函数中实现,另一种方法在函数体中本地创建并从闭包中实现。

您是否尝试实现处理程序并检查可能的错误?