jeudi 28 juillet 2016

OHHTTPStubs not stubbing network call

I am fairly new to unit testing network calls and I am going down the OHHTTPStubs route, Although I am not entirely sure how to set it up. Below is my code but the call is not returning the error I am trying to stub.

let expectation = expectationWithDescription("AddRoomErrorTest")

    stub(isHost("https://api.com")) { (_) -> OHHTTPStubsResponse in
        return OHHTTPStubsResponse(error: NSError(domain:NSURLErrorDomain, code:Int(CFNetworkErrors.CFURLErrorNotConnectedToInternet.rawValue), userInfo:nil))
    }

    let path = "/.../.../"
    let dictionary: NSDictionary = ["body": ""]

    CommonNewservice.sharedInstance.post(path, requestObject: dictionary as? Dictionary<String, AnyObject>) { (successObject: AnyObject!, errorObject: AnyObject!) in

        XCTAssertNotNil(errorObject)

        expectation.fulfill()

    }

    waitForExpectationsWithTimeout(5) { error in
        XCTAssertNil(error, "\(error)")
    }

It should return a error but error is being returned nil :S

Thanks for any help in advance

Aucun commentaire:

Enregistrer un commentaire