【问题标题】:IOS: Unit testing using dispatch_async is not executingIOS:未执行使用 dispatch_async 的单元测试
【发布时间】:2016-02-09 15:37:06
【问题描述】:

我写了一个类似于下面代码的脚本。我在“dispatch_async”中保留了断点,但它没有被执行。这可能是什么问题?

 dispatch_async(dispatch_get_main_queue()) {

    let array = response!.allObjects as! [NSArray]    
   XCTAssertNotEqual(array.count, 0, testPassed);
   self.waitForExpectationsWithTimeout(10, handler: { (error: NSError?) -> Void in
       if(error != nil) {
          XCTFail("Failed with error: \(error)
      }
     })


 }

【问题讨论】:

    标签: ios swift unit-testing xctest xctestexpectation


    【解决方案1】:

    这个dispatch_async 在单元测试中没有意义。单元测试正在主队列上运行。在单元测试完成之前它不会释放队列来运行另一个块,此时运行这个块为时已晚。

    正如所写,唯一有意义的方法是完全删除dispatch_async。你在主线程上。只需执行您想要的代码即可。

    【讨论】:

    • 感谢 Rob 的快速回复。当我删除 dispatch_async 时,单元测试脚本崩溃了。我在“stackoverflow.com/questions/33576790/…”中问了一个问题。不知道是什么导致了崩溃......如果我执行一个测试功能,它工作正常。但是,如果我为整个项目(近 1000 个测试)运行测试脚本,它就会崩溃。
    猜你喜欢
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 2016-04-15
    • 2019-05-29
    • 2018-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多