Created
January 29, 2019 11:26
-
-
Save charlieInDen/da3d66c401d2326f1fc0e081befb0924 to your computer and use it in GitHub Desktop.
Unit test :- Prepare URLRequest -> Parse Response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Unit test :- Prepare URLRequest -> Parse Response | |
class PointOfInterestRequestTests: XCTestCase { | |
let request = PointsOfInterestRequest() | |
func testMakingURLRequest() throws { | |
let coordinate = CLLocationCoordinate2D(latitude: 37.3293, longitude: -121.8893) | |
let urlRequest = try request.makeRequest(from: coordinate) | |
XCTAssertEqual(urlRequest.url?.scheme, "https") | |
XCTAssertEqual(urlRequest.url?.host, "example.com") | |
XCTAssertEqual(urlRequest.url?.query, "lat=37.3293&long=-121.8893") | |
} | |
func testParsingResponse() throws { | |
let jsonData = "[{\"name\":\"My Location\"}]".data(using: .utf8)! | |
let response = try request.parseResponse(data: jsonData) | |
XCTAssertEqual(response, [PointOfInterest(name: "My Location")]) | |
} | |
} | |
PointOfInterestRequestTests.defaultTestSuite.run() | |
//Integration test:- Prepare URLRequest -> Create URLSession Task -> Parse Response | |
class APILoaderTests: XCTestCase { | |
var loader: APIRequestLoader<PointsOfInterestRequest>! | |
override func setUp() { | |
let request = PointsOfInterestRequest() | |
let configuration = URLSessionConfiguration.ephemeral | |
configuration.protocolClasses = [MockURLProtocol.self] | |
let urlSession = URLSession(configuration: configuration) | |
loader = APIRequestLoader(apiRequest: request, urlSession: urlSession) | |
} | |
func testLoaderSuccess() { | |
let inputCoordinate = CLLocationCoordinate2D(latitude: 37.3293, longitude: -121.8893) | |
let mockJSONData = "[{\"name\":\"MyPointOfInterest\"}]".data(using: .utf8)! | |
MockURLProtocol.requestHandler = { request in | |
XCTAssertEqual(request.url?.query?.contains("lat=37.3293"), true) | |
return (HTTPURLResponse(), mockJSONData) | |
} | |
let expectation = XCTestExpectation(description: "response") | |
loader.loadAPIRequest(requestData: inputCoordinate) { pointsOfInterest, error in | |
XCTAssertEqual(pointsOfInterest, [PointOfInterest(name: "MyPointOfInterest")]) | |
expectation.fulfill() | |
} | |
wait(for: [expectation], timeout: 1) | |
} | |
} | |
APILoaderTests.defaultTestSuite.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment