I have Swift 2 code using Alamofire that requests a remote url that it expects to return JSON. I have done enough interactive testing to be sure that it behaves correctly enough. Now I want to test it so that I can rely on this code and then move on to other things.
I am new to Swift, although I have been programming OO Perl for decades, so maybe something that I missed.
This is how I set the verification code so far (non-essential things such as how I delete the username and password to avoid yak shaving). The calling code indicates the username and password and expects one of the two callbacks to be called whenever a result is received. (I have not reached the point of errors, so please do not pay attention to this topic.)
struct checkUsernameHandler { var onSuccess: () -> () var onFailure: () -> () } struct checkUsernameOutput { var authenticated: Bool? } func checkUsername(user: User, handler: checkUsernameHandler) { Alamofire.request(.PUT, NSURL(string: baseURL + "/user/verify?key=" + apiKey)!, parameters: [ "username": user.username!, "password": user.password!, ], encoding: .JSON ).responseJSON { response in let checkusernameOutput = self.analyseCheckUsernameResponse(response) if let authenticated = checkusernameOutput.authenticated { if authenticated { handler.onSuccess() } else { handler.onFailure() } } } } func analyseCheckUsernameResponse (response: Response<AnyObject, NSError>) -> checkUsernameOutput { var output = checkUsernameOutput() if (response.result.isSuccess) { if let JSON = response.result.value as? NSDictionary {
What I'm looking for is a way to take raw JSON and somehow pass it to Alamofire so that I can create a response object that has the appropriate stuff, including errors if it doesn't receive anything (timeout?) Or The data returned was invalid (for example, HTML 404 or 500 pages).
At the moment, I'm quite happy with the checkUsername function, so I'm not interested in someone saying "use OHHTTPStubs". I am looking at testing only the code that I doubt it is the code I just wrote.
EDIT . Fearing something else, it seems that the inside of Alamofire is sufficiently connected with the fact that in order to create a fake answer I need to do almost all the work related to setting up a network connection one way or another. Therefore, perhaps the answer: "Is it too much trouble to do this"?
source share