我试图用XCode编写单元测试,但是遇到异步函数问题。主要思想是检查接收到的JSON的长度
我要测试的功能(在someVC类中):
func getSomeCount(url: String){
Alamofire.request(url, method: .get).responseJSON { (response) in
if response.result.isSuccess{
self.someJSON = JSON(response.result.value!)
self.length = self.someJSON!.count
}else{
self.length = 0
print("Error \(String(describing: response.result.error))")
}
}
}
测试看起来像这样:
func testAPIResponse(){
let someVC = SomeVC()
someVC.getSomeCount(url: "http:\\www.someapi.com")
XCTAssertEqual(someVC.length, 1234)
}
我知道我应该使用期望值,但是在这种情况下不知道如何正确实现它们。测试失败,说0不等于1234。应该如何正确完成?