Skip to content

Instantly share code, notes, and snippets.

@arbo-hacker
Last active October 27, 2017 22:15
Show Gist options
  • Save arbo-hacker/2dbd5f59e9babb70e801f9ab7987d439 to your computer and use it in GitHub Desktop.
Save arbo-hacker/2dbd5f59e9babb70e801f9ab7987d439 to your computer and use it in GitHub Desktop.
Como escribir una prueba unitaria para una funcion que usa Alamofire
func getAllData(_ completion: @escaping (_ error: NSError?, _ json: String?) -> Void) {
Alamofire.request("https://api.arbo.com.ve/test_api/getalldata").validate().responseJSON { (response) in
do {
let json = JSON(data: response.data!)
completion(json)
}
}
}
import XCTest
class TestsFail: XCTestCase {
func testGettingJSON() {
Request.sharedInstance.getAllData { (error, result) in
XCTAssertNil(error)
XCTAssertNotNil(result)
ex.fulfill()
}
}
}
import XCTest
class TestsSuccess: XCTestCase {
func testGettingJSON() {
let ex = expectation(description: "Expecting a JSON data not nil")
Request.sharedInstance.getAllData { (error, result) in
XCTAssertNil(error)
XCTAssertNotNil(result)
ex.fulfill()
}
waitForExpectations(timeout: 10) { (error) in
if let error = error {
XCTFail("error: \(error)")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment