Instantly share code, notes, and snippets.

Embed
What would you like to do?
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