我第一次做单元测试,有些具体的案例让我迷路了。
尽管读了很多书,我还是不知道如何在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.com/documentation/xctest/asynchronous_tests_and_expectations/testing_asynchronous_operations_with_expects?language=objc),但我不知道如何使用它们。 我猜我应该必须调用expectation.fulfull(),但我不知道在哪里。
由于MyFunction
不接受可以在其中调用Fulfult
的完成处理程序,因此它是不可测试的。
您将需要重构函数以接受可选的完成处理程序闭包,并在工作完成后调用它。 然后,当您从测试中调用函数时,您可以传递一个闭包,在该闭包中,您可以实现
期望。