我是第一次接触单元测试,对于一些具体的案例我很迷茫。
尽管读了很多书,但我对如何在iOS中测试这样的功能感到困惑。
func myFunction() {
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
// Tasks to do in background
dispatch_async(dispatch_get_main_queue(), ^(void){
// Get the results
// Update UI
});
});
}
我读过关于 "期望 "的文章(https:/developer.apple.comdocumentationxctestasynchronous_tests_and_expectationstesting_asynchronous_operations_with_expectations?语言=objc。),但我不知道如何使用它们。我想我应该要callexpectation.fill(),但我不知道在哪里。
自 myFunction
不接受一个完成处理程序,你可以在其中调用 fulfill
它是不可测试的。
你需要重构函数,接受一个可选的完成处理程序闭包,并在工作完成后调用它。然后,当你从测试中调用函数时,你可以传递一个闭包,在其中你可以做到 fulfill
的期望。