- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
重要事实
I forgot to mention an important factor in the question. I am running this in a TestCase. I think this issue has something to do with the TestCase not awaiting for async completionHandler to return
从Alamofire
迁移出来至 SwiftHTTP
,因为我发现这更容易。
在 SwiftHHTP 上,没有办法知道生成了什么 URL,它返回了什么错误。例如,我试图查看 opt.debugDescription
变量,它返回了一些神秘的东西,比如 description String "<SwiftHTTP.HTTP: 0x60000007e540>"
我遵循的步骤
YES
至 Allow Arbitrary Loads
. fullurl ->http://120.0.0.1:8080/myapi/Driver/getDriver?driver=2243&domain=4345&key=asdfasdf
,iPhone 模拟器上的 Safari 会以正确的 JSON 响应.连catalina.out
在我的 Mac 上运行的 tomcat 服务器以调试消息响应。但是当我在 Xcode 下的测试用例中运行它时,下面的代码没有打印调试打印。
代码
var getData = [String:String]()
getData = ["domain": "4345",
"driver" : "2343",
"key" : "asdfasdf"]
var urlComponents = URLComponents(string: fullURL)!
var queryItems = [URLQueryItem]()
queryItems = self.getData.map{ URLQueryItem(name : $0.0, value : $0.1) }
urlComponents.queryItems = queryItems
print("fullurl ->"+(urlComponents.url)!.absoluteString)
do {
let opt = try HTTP.GET((urlComponents.url)!.absoluteString)
opt.start { response in
if let err = response.error {
print("--1-> error: \(err.localizedDescription)")
return //also notify app of failure as needed
}
print("--2--> opt finished: \(response.description)")
self.responseData = response
}
} catch let error {
print("--3--> got an error creating the request: \(error)")
}
编辑
即使将代码更改为 https
或 http://www.google.com
, 结果相同。
let testComponents = URLComponents(string: "https://www.google.com")!
URLSession.shared.dataTask(with: (testComponents.url)!, completionHandler: {
(data, response, error) in
if(error != nil){
print("..1>..")
}else{
do{
print ("..2>.." )
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String : AnyObject]
self.responseData = json
}catch let error as NSError{
print("..3>..")
}
}
}).resume()
编辑 1从 here 试过@Vivek 的回答。
callWebService(url: (urlComponents.url)!.absoluteString)
.
.
func callWebService(url : String) {
.
.
let callURL = URL.init(string: url)
没有再次打印,错误/JSON,没有。
最佳答案
是的,默认情况下,单元测试不会等待 completionHandler
被调用。如果在测试中调用异步函数,则不需要更改函数的代码,而是更改测试的行为。
解决方案:XCTestExpectation
在您的测试类(XCTest
的子类)中,添加此属性:
var expectation: XCTestExpectation?
异步请求的测试函数基本上如下所示:
func testRequest() {
expectation = expectation(description: "Fetched sites") //1
//2
some.asyncStuffWithCompletionHandler() {
someData in
if someData == nil {
XCTestFail("no data") //3
return
}
//looks like the request was successful
expectation?.fulfill() //4
}
//5
waitForExpectations(timeout: 30, handler: nil)
}
这定义了您希望测试代码执行的操作。但实际上,添加什么作为描述并不重要。这只是给你的信息,在运行测试时
这是带有completionHandler
的函数,您正在调用
如果想让测试在completionHanlder
内失败,调用XCTestFail()
如果 completionHandler
中的所有内容都按预期工作,则通过调用 expectation?.fulfill
来实现 expectation
。
重要的部分来了:这部分代码将在 completionHandler
之前执行!如果这是函数的结束,则测试将停止。这就是为什么我们告诉测试要等到预期得到满足(或经过一定时间)
有一个有趣的blog post关于单元测试。 (参见“XCTestExpectation”部分)它是用旧的 Swift 语法编写的,但概念是相同的。
关于json - 测试用例 : SwiftHTTP library not making the HTTP call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42481033/
我尝试解析来自这样的服务器的 json 响应 HTTP.GET(ServerPatientApi.SPLASH, parameters: nil) { response in
我正在使用 SwiftHTTP在 iOS 应用程序中执行 Web 服务器中的基本身份验证。如果身份验证成功,一切都会按预期进行。但是,如果身份验证失败(代码为 401),调用失败处理程序大约需要 35
我在 swift 中创建了一个新的 Xcode 7 项目。然后我退出 Xcode,并创建 pod 文件。 pod init 然后我使用提供的配置编辑文件: source 'https://github
我将 SwiftHTTP.xcodeproject 添加到我的项目中, 在构建阶段:我添加 4 个 SwiftHTTP.framework ViewController --- 导入 SwiftHTT
我突然开始观察到我的一个应用程序的新更新出现以下崩溃。它似乎经常发生,并且令人困惑,因为我在过去 6 个月内没有对代码的这一部分进行任何更改。我确实想知道 this (EXC_BAD_ACCESS 返
我正在使用 swiftHTTP 向我的服务器发出请求,当我的互联网连接速度较慢时,它会转到响应部分!我在下面设置了示例代码: HTTP.GET("myURL") { response in let
我正在尝试在 Swift 中创建一个 Twitter 时间轴。我正在使用 SimpleAuth 以及 SwiftHTTP。我在另一个类中成功授权了 Twitter 并获得了访问 token 和用户 I
我正在尝试学习如何将 swiftHTTP 与 mishap api ( https://www.mashape.com/textanalysis/textanalysis ) 一起使用。到目前为止,这
重要事实 I forgot to mention an important factor in the question. I am running this in a TestCase. I thi
我是一名优秀的程序员,十分优秀!