Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

propose solution #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
77 changes: 42 additions & 35 deletions ThrowingAsyncErrorTests/ThrowingAsyncErrorTests.swift
Original file line number Diff line number Diff line change
@@ -1,58 +1,65 @@
//
// ThrowingAsyncErrorTests.swift
// ThrowingAsyncErrorTests
//
// Created by Jasper Visser on 06/11/2019.
// Copyright © 2019 Jasper Visser. All rights reserved.
//
//

import XCTest
@testable import ThrowingAsyncError

class ThrowingAsyncErrorTests: XCTestCase {

func testSyncError() {
XCTAssertThrowsError(ThrowingStruct())
}

func testAsyncErrorWithDeallocation() {
let exp = expectation(description: "")
var instance: ThrowingStructAfter2Seconds? = ThrowingStructAfter2Seconds()

Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false) { (_) in
instance = nil

exp.fulfill()
}

waitForExpectations(timeout: 3, handler: nil)
XCTAssertThrowsError(try Thrower("I WILL THROW"))
}

func testAsyncErrorWithoutDeallocation() {
let exp = expectation(description: "")
let instance = ThrowingStructAfter2Seconds()

// How do I catch the error thrown by instance initializer?
// I want to test to see if it crashed

waitForExpectations(timeout: 3, handler: nil)

func testAsyncError() {
let thrower = Thrower(test1: "I WILL THROW LATER")
let futureErrorExpectation = thrower.newFutureErrorExpectation

wait(for: [futureErrorExpectation], timeout: 5)
}
}


/// Make a protocol to enforce testability
protocol FutureErrorTestable: class {
var futureErrorExpectation: XCTestExpectation? { get set }
}

struct ThrowingStruct {
init() {
fatalError()
/// Encapsulate expectation generation for testing
extension FutureErrorTestable {
var newFutureErrorExpectation: XCTestExpectation {
let newExpectation = XCTestExpectation(description: "\(self) throws error")
futureErrorExpectation = newExpectation
return newExpectation
}
}

class ThrowingStructAfter2Seconds {
init() {
class Thrower: FutureErrorTestable {
init() { }

/// Throw an error immediately
convenience init(_ test: Any? = nil) throws {
self.init()
throw NowError.some
}

/// Throw an error after 1 second
convenience init(test1: Any? = nil) {
self.init()
Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { [weak self] (_) in
if self != nil {
fatalError()

self?.futureErrorExpectation?.fulfill()
}
}
}

/// You have to store an expectation to make asynchronous nested blocks fulfillable, because they can't throw
var futureErrorExpectation: XCTestExpectation?
}

enum FutureError: Error {
case some
}
enum NowError: Error {
case some
}