2

I have the following code in my project:

import PromiseKit
import SwiftyJSON

class ChartHandler {
var alamofireManager: Alamofire.Manager?

init() {
    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    configuration.timeoutIntervalForRequest = 30

    self.alamofireManager = Alamofire.Manager(configuration: configuration)
}

func requestTabsJSONWithLinkKey(linkKey: String) -> Promise<JSON> {
    return Promise { fulfill, reject in

        alamofireManager?.request(.GET, "https://.../\(linkKey)/").responseJSON {
            response in

            switch response.result {
            case .Success:
                if let value = response.result.value {
                    fulfill(JSON(value))
                }
            case .Failure(let error):
                if error.code == NSURLErrorTimedOut {
                    reject(RequestResult.TimedOut)
                } else {
                    reject(RequestResult.ConnectionFailed)
                }
            }
        }
    }
}
}

And the unit test for this function using Mockingjay framework

func testChartHandlerTabsRequest() {
    let expectation = expectationWithDescription("tabs json test")
    let body = ["json": "test"]
    stub(everything, builder: json(body))

    chartHandler.requestTabsJSONWithLinkKey("test_link_key").then { jsonFromRequest -> Void in
        print(jsonFromRequest)
        XCTAssertTrue(jsonFromRequest.dynamicType == JSON([String: AnyObject]()).dynamicType, "Pass")
        expectation.fulfill()
    }.error { err in
        print("Error: \(err)")
    }
    waitForExpectationsWithTimeout(5.0, handler: nil)
}

Problem is the following: the execution going into "error" branch of code instead of "then" it is result of the stubing is not working properly in that case and i dont know how to figure it out. Any suggestions?

Andrey M.
  • 3,021
  • 29
  • 42

0 Answers0