This file contains hidden or 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
func test_cancellAllTasks_givenExistsActiveTasks_shouldCancellAllOfThem() { | |
URLSession.initSwizzle() | |
let sut = Network.shared | |
let taskLorem = URLSessionTaskSpy() | |
let taskIpsum = URLSessionTaskSpy() | |
FakeURLSession.tasksToBeReturned = [taskLorem, taskIpsum] | |
XCTAssertFalse(taskLorem.cancelCalled) |
This file contains hidden or 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
class FakeURLSession: URLSession { | |
static var tasksToBeReturned: [URLSessionTask] = [] | |
@objc func _getAllTasks(completionHandler: @escaping ([URLSessionTask]) -> Void) { | |
completionHandler(Self.tasksToBeReturned) | |
} | |
} |
This file contains hidden or 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
extension URLSession { | |
class func initSwizzle() throws { | |
typealias ResultBlock = (@escaping ([URLSessionTask]) -> Void) -> Void | |
let original = #selector((URLSession.getAllTasks(completionHandler:)) as (URLSession) -> ResultBlock) | |
let swizzled = #selector((FakeURLSession._getAllTasks(completionHandler:)) as (FakeURLSession) -> ResultBlock) | |
guard | |
let originalMethod = class_getInstanceMethod(URLSession.self, original), | |
let swizzledMethod = class_getInstanceMethod(FakeURLSession.self, swizzled) |
This file contains hidden or 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
extension URLSession { | |
class func initSwizzle() { | |
// TODO | |
} | |
} |
This file contains hidden or 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
@available(iOS 7.0, *) | |
open class URLSession : NSObject { | |
open class var shared: URLSession { get } | |
public /*not inherited*/ init(configuration: URLSessionConfiguration) | |
public /*not inherited*/ init(configuration: URLSessionConfiguration, delegate: URLSessionDelegate?, delegateQueue queue: OperationQueue?) | |
open var delegateQueue: OperationQueue { get } |
This file contains hidden or 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
func test_cancellAllTasks_givenExistsActiveTasks_shouldCancellAllOfThem() { | |
let session = URLSessionSpy() | |
let sut = Network(session: session) | |
let loremTask = URLSessionDataTaskSpy() | |
let ipsumTask = URLSessionDataTaskSpy() | |
session.tasksToBeReturned = [loremTask, ipsumTask] | |
sut.cancelAllTasks() | |
This file contains hidden or 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
final class URLSessionDataTaskSpy: URLSessionDataTask { | |
private(set) var cancelCalled: Bool = false | |
override func cancel() { | |
cancelCalled = true | |
} | |
} |
This file contains hidden or 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
final class URLSessionSpy: URLSession { | |
var tasksToBeReturned: [URLSessionTask] = [] | |
override func getAllTasks(completionHandler: @escaping ([URLSessionTask]) -> Void) { | |
completionHandler(tasksToBeReturned) | |
} | |
} |
This file contains hidden or 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
func test_cancellAllTasks_givenExistsActiveTasks_shouldCancellAllOfThem() { | |
... | |
let cancelTasksExpectation = XCTestExpectation(description: "cancelTasksExpectation") | |
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { | |
sut.session.getAllTasks { tasks in | |
activeTasksCount = tasks.count | |
cancelTasksExpectation.fulfill() |
This file contains hidden or 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
func test_cancellAllTasks_givenExistsActiveTasks_shouldCancellAllOfThem() { | |
... | |
let getTasksExpectation = XCTestExpectation(description: "getTasksExpectation") | |
var activeTasksCount: Int? | |
sut.session.getAllTasks { tasks in | |
activeTasksCount = tasks.count |
NewerOlder