I'm trying to mock HTTP requests in my Quick test case. In order to do so I've adjusted a couple of things. I subclassed the QuickSpec to add loading functionality in my test cases like so:
HTTPMocks.swift
import Foundation
class HTTPMocks: NSURLProtocol {
override class func canInitWithRequest(request: NSURLRequest) -> Bool {
println("############################")
return true
}
override class func canonicalRequestForRequest(request: NSURLRequest) -> NSURLRequest {
return request
}
override class func requestIsCacheEquivalent(a: NSURLRequest, toRequest b: NSURLRequest) -> Bool {
return a.URL == b.URL
}
override func startLoading() {
println("WHAT ")
}
}
CSpec.swift
class CSpec: QuickSpec {
override class func initialize(){
super.initialize()
struct Static {
static var token: dispatch_once_t = 0
}
dispatch_once(&Static.token){
CSpec.setup()
}
}
class func setup() {
NSURLProtocol.registerClass(HTTPMocks)
}
}
I'm using my CSpec class in the following test case
ProjectAPISpec.swift
import Foundation
import Quick
import Nimble
class ProjectAPISpec: CSpec {
override func spec() {
describe(".getOrders"){
func subject() -> Void {
waitUntil { done in
ProjectAPI.getOrders(1){ (orders) in
done()
}
}
}
it("works"){
subject()
expect(true).to(beTruthy())
}
}
}
}
to test the following code
ProjectAPI.swift
import Alamofire
let host: String = "<HOST>"
let token: String = "<TOKEN>"
class ProjectAPI {
class func getOrders(page: Int, completionHandler: ([Product]) -> Void) {
Alamofire.request(.GET, "http://\(host)/api/orders.json", parameters: ["page": page, "token": token])
.response{ (request, response, data, error) in
completionHandler([])
}
}
}
The implementation is not relevant and incomplete, but here's my question:
Why when I run the test, the callbacks of HTTPMock are not being triggered?
Aucun commentaire:
Enregistrer un commentaire