HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 在单元测试中处理MainThread执行大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_489_0@
@H_489_0@
我有一个后台运行的函数,完成后它会更新主线程中的UI.我注意到当代码到达主线程的调用时单元测试失败.我该如何纠正这个?

例如
注意:long描述了项目中的伪逻辑,而不是确切的代码

在主要代码中:

func getResponse(identifier : String,completion :(success :Bool)->){
   // uses identifier to request data via api and on completion:
   completion(status: truE)
}

testObject.getResponse(wantedvalue){(success) in
    if status == true {
        dispatch_async(dispatch_get_main_queue()){
           self.presentViewController(alertController,animated: true,completion: nil)

           }
     } 
}

并在单元测试中

func testGetResponse(){
     var testObject = TestObject()
     var expectation = self.self.expectationWithDescription("Response recieved")

     testObject.getResponse(wantedvalue){(success) in
          expectation.fulfill()
     } 

     self.waitForExpectationsWithTimeout(10) { (error) in
        XCTAssertTrue(testViewController.presentedViewController as? CustomViewController)
     }
}

这似乎是一个潜在的僵局,但我不确定如何解决它.

解决方法

waitForExpectationsWithTimeout也是用于未调用异步函数或未正确完成的情况的回退方法(因此未调用fulfill()方法).

尝试检查错误对象.

我建议在进行fullfill()调用之前进行验证.

请参阅以下Swift 3的示例代码,了解如何使用fullfill和waitForExpectationsWithTimeout.

func testGetResponse(){

    var testObject = TestObject()
    var validationExpectation = expectation(description: "Response received")

    testObject.getResponse(wantedvalue){(success) in
        // Do your validation
        validationExpectation.fulfill()
        // Test succeeded
    }

    waitForExpectationsWithTimeout(60) { (error) in
        if let error = error {
            // Test Failed
            XCTFail("Error: \(error.localizedDescription)")
        }
    }
}
@H_489_0@

大佬总结

以上是大佬教程为你收集整理的ios – 在单元测试中处理MainThread执行全部内容,希望文章能够帮你解决ios – 在单元测试中处理MainThread执行所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:iosmainthread单元处理执行测试
猜你在找的HTML5相关文章
其他相关热搜词更多
phpJavaPython程序员load如何string使用参数jquery开发安装listlinuxiosandroid工具javascriptcap