Skip to content

Instantly share code, notes, and snippets.

class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let vc = StarsViewTestVC()
window!.rootViewController = vc
return true
}
protocol ThingsService {
func getThings(onCompletion callback: (Result<[Thing]>) -> Void)
func add(onCompletion callback: (Result<Bool>) -> Void)
}
class FakeThingsService: ThingsService {
private(set) var things: [Thing]
init(things: [Thing]) {
self.things = things
protocol ThingsServiceDelegate {
func service(_ ThingsService, didFinishWithResult: Result<[Thing]>)
}
class ThingsService {
private var delegate: ThingsServiceDelegate
init(with delegate: ThingsServiceDelegate) {
self.delegate = delegate
}
func getThings(onCompletion callback: (Result<[Thing]>) -> Void) { ... }
let successResult: Result<[Thing]> = .success([aThing, anotherThing])
let failureResult: Result<[Thing]> = .failure(Error(...))
let successResult: ([Thing]?, Error?) = ([aThing, anotherThing], nil)
let failureResult: ([Thing]?, Error?) = (nil, Error(...))
extension URLSession {
func sendSynchronousRequest(request: URLRequest) -> (Data?, URLResponse?, Error?) {
let semaphore = DispatchSemaphore(value: 0)
var result: (data: Data?, response: URLResponse?, error: Error?)
let task = self.dataTask(with: request) {
result = (data: $0, response: $1, error: $2)
semaphore.signal()
}
task.resume()
protocol ThingsService {
func getThings() -> Result<[Thing]>
func add(thing: Thing) -> Result<Bool>
}
class FakeThingsService: ThingsService {
private(set) var things: [Thing]
init(things: [Thing]) {
self.things = things
}
class ServicesTest: XCTestCase {
let service = ProfileService()
setup() {
// handle authentication if needed
// let authService = ...
// let authMethod = ...
// authService.auth(authMethod)
}
func startApp() {
onMainDo({
self.activityIndicator.startAnimating()
}, onBackgroundDo: {
let storage = PersistentSecureStorage.shared()
guard let savedToken == storage.token {
return .goToAuth
}
let tokenIsValid = authService.isValidToken(savedToken)
if !tokenIsValid {