0
Im單元現在測試我的Alamofire代碼。我試圖創建一個可重用的方法,我可以傳入一個狀態碼和一個需要被調用爲參數的方法。我正在考慮使用閉區塊,但語法正在逃避我,它並沒有幫助我開始使用Swift。這裏有一個例子:插入函數作爲參數
func parseJsonResponse(response: Response <AnyObject, NSErrror>){
//parsing
}
func test_networkRequest(withStatusCode statusCode: Int, /* parse function as parameter */) {
stub(isHost("https://httpbin.org")) { _ in
return OHHTTPStubsResponse(fileAtPath:OHPathForFile("wsresponse.json", self.dynamicType)!,
statusCode:statusCode, headers:["Content-Type":"application/json"])
})
Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
.responseJSON { response in
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
XCTAsertNotNil(response)
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
//method should go here
self.parseJsonResponse(response)
}
}
我希望能夠讓「test_networkRequest」可重複使用的在我所有的課程,會有不同類型的JSON進行解析,並以不同的方式處理。這就是爲什麼我希望能夠將一個函數作爲參數傳遞給'test_networkRequest'的原因。
我希望我的最終目標是明確的,如果我偏離軌道,我願意接受建議。 :)