1
我正在嘗試使用OHHTTPStubs和Quick/Nimble來測試Alamofire請求存根響應。但Alamofire不處理響應,因此我無法測試結果。無法使用Alamofire測試存根響應
我目前的測試代碼是:
OHHTTPStubs.stubRequestsPassingTest({$0.URL!.host == "authenticate.com"}, withStubResponse: { (request: NSURLRequest) -> OHHTTPStubsResponse in
let obj = ["status": "ok", "data": "something"]
return OHHTTPStubsResponse(JSONObject: obj, statusCode:200, headers:nil)
})
let gitdoRepository: GitdoRepository = GitdoRepository()
waitUntil(timeout: 2, action: { (done) -> Void in
gitdoRepository.authenticate("http://authenticate.com", completion: { (error) ->() in
expect(error).toNot(beNil())
})
NSThread.sleepForTimeInterval(2)
done()
})
在那裏我已經在存根封增加了斷點,以確保Alamofire執行請求和閉合被調用。然而,客戶端的響應關閉從不會被調用,因此測試無法成功運行。這是驗證方法:
func authenticate(authenticationUrl: String, completion: (error: OauthError?) ->()) {
Alamofire.request(.POST, authenticationUrl).responseJSON { (request: NSURLRequest?, response: NSHTTPURLResponse?, object: AnyObject?, error: NSError?) -> Void in
if let error = error {
completion(error: .HTTPError(error))
}
else if let object = object {
if let oauth = self.oauthParser(object) {
self.oauth = oauth
completion(error: nil)
}
else {
completion(error: .UnparseableCredentials)
}
}
else {
completion(error: .ResponseWithoutCredentials)
}
}
}
有什麼我做錯了Alamofire? 在此先感謝