Created
January 29, 2019 11:26
-
-
Save charlieInDen/8d2ce8f17ef1424e317bfe65f4285d75 to your computer and use it in GitHub Desktop.
Unit test :- Prepare URLRequest -> Parse Response, Integration test:- Prepare URLRequest -> Create URLSession Task -> 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