Created
April 24, 2021 18:58
-
-
Save bocato/46624ef0c32a7b6772e7b1823e208bcf to your computer and use it in GitHub Desktop.
testing_fatal_error_3.swift
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
import XCTest | |
final class AnyEquatableTests: XCTestCase { | |
func test_unsafelyUnwrapped_whenWrongTypeIsPassed_itShouldThrowFatalError() { | |
// Given | |
let mockState: String = "StateMock" | |
let failureHandlerExpectation = expectation(description: "Expecting failureHandler.") | |
var assertionMessage: String? | |
let failureHandler: (@autoclosure () -> String, StaticString, UInt) -> Never = { message, _, _ in | |
assertionMessage = message() | |
failureHandlerExpectation.fulfill() | |
self.unreachable() | |
} | |
let sut = AnyEquatable( | |
erasing: mockState, | |
failureHandler: failureHandler | |
) | |
let expectedType = String(describing: sut.erasedValue) | |
let typePassed = String(describing: Int.self) | |
let expectedMessage = "Trying to cast \(expectedType) to \(typePassed)!" | |
// When | |
let testCase: () -> Void = { | |
_ = sut.unsafelyUnwrapped() as Int | |
} | |
DispatchQueue.global(qos: .userInitiated).async(execute: testCase) | |
// Then | |
waitForExpectations(timeout: 2.0) { _ in | |
XCTAssertEqual(assertionMessage, expectedMessage) | |
} | |
} | |
// MARK: - Helpers | |
private func unreachable() -> Never { | |
repeat { | |
RunLoop.current.run() | |
} while true | |
} | |
} | |
AnyEquatableTests.defaultTestSuite.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment