Skip to content

Instantly share code, notes, and snippets.

View charlieInDen's full-sized avatar
😈
In a den

Nishant Sharma charlieInDen

😈
In a den
View GitHub Profile
@charlieInDen
charlieInDen / UnitTesting.playground
Created January 29, 2019 11:15
Sample code for making request to server with query parameter and then parsing data and updating a view
import CoreLocation
struct PointOfInterest: Codable, Equatable {
var name: String
}
class PointOfInterestSample {
var tableView: UITableView = UITableView()
var tableValues:[PointOfInterest]?
func handleError(_ error: Error?) -> Void {
print(error!)
@charlieInDen
charlieInDen / UnitTesting.playground
Created January 29, 2019 11:21
API Request Protocol - Code restructuring so that unit test cases can be written for code
protocol APIRequest {
associatedtype RequestDataType
associatedtype ResponseDataType
func makeRequest(from data: RequestDataType) throws -> URLRequest
func parseResponse(data: Data) throws -> ResponseDataType
}
@charlieInDen
charlieInDen / UnitTesting.playground
Created January 29, 2019 11:22
Restructuring code to write unit test cases - Prepare URLRequest, Parse Response
struct PointsOfInterestRequest: APIRequest {
enum RequestError: Error {
case invalidCoordinate
case unknown
}
func makeRequest(from coordinate: CLLocationCoordinate2D) throws -> URLRequest {
guard CLLocationCoordinate2DIsValid(coordinate) else {
throw RequestError.invalidCoordinate
}
var components = URLComponents(string: "https://example.com/locations")!
@charlieInDen
charlieInDen / UnitTesting.playground
Last active January 29, 2019 11:25
Restructuring code for Create URLSession Task and make request to server and parse the data , It will help in writing Integration test cases
class APIRequestLoader<T: APIRequest> {
let apiRequest: T
let urlSession: URLSession
init(apiRequest: T, urlSession: URLSession = .shared) {
self.apiRequest = apiRequest
self.urlSession = urlSession
}
func loadAPIRequest(requestData: T.RequestDataType,
@charlieInDen
charlieInDen / gist:da3d66c401d2326f1fc0e081befb0924
Created January 29, 2019 11:26
Unit test :- Prepare URLRequest -> Parse Response
//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")
@charlieInDen
charlieInDen / UnitTesting.playground
Created January 29, 2019 11:26
Unit test :- Prepare URLRequest -> Parse Response, Integration test:- Prepare URLRequest -> Create URLSession Task -> Parse Response
//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")
@charlieInDen
charlieInDen / UnitTesting.playground
Created January 30, 2019 03:22
URL loading system
class MockURLProtocol: URLProtocol {
static var requestHandler: ((URLRequest) throws -> (HTTPURLResponse, Data))?
override func startLoading() {
guard let handler = MockURLProtocol.requestHandler else {
XCTFail("Received unexpected request with no handler set")
return
}
do {
let (response, data) = try handler(request)
client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
@charlieInDen
charlieInDen / UnitTesting.playground
Created January 30, 2019 11:39
Default implementation of sending notification and adding observer before restructuring to write test cases
class CurrentLocationProvider {
static let authChangedNotification = Notification.Name("AuthChanged")
func notifyAuthChanged() {
let name = CurrentLocationProvider.authChangedNotification
NotificationCenter.default.post(name: name, object: self)
}
}
class PointsOfInterestTableViewController {
@charlieInDen
charlieInDen / UnitTesting.playground
Created January 30, 2019 11:42
After restructuring for writing test cases for NSNotification centre logic
class CurrentLocationProvider {
static let authChangedNotification = Notification.Name("AuthChanged")
let notificationCenter: NotificationCenter
init(notificationCenter: NotificationCenter = .default) {
self.notificationCenter = notificationCenter
}
func notifyAuthChanged() {
let name = CurrentLocationProvider.authChangedNotification
notificationCenter.post(name: name, object: self)
@charlieInDen
charlieInDen / UnitTesting.playground
Created January 30, 2019 11:46
NSNotificationCentre Unit test cases
class PointsOfInterestTableViewControllerTests: XCTestCase {
func testNotification() {
let notificationCenter = NotificationCenter()
let observer = PointsOfInterestTableViewController(notificationCenter:
notificationCenter)
XCTAssertFalse(observer.didHandleNotification)
// Notification posted to just this center, isolating the test
let name = CurrentLocationProvider.authChangedNotification
notificationCenter.post(name: name, object: nil)
XCTAssertTrue(observer.didHandleNotification)