Created
March 14, 2021 18:58
-
-
Save lukaskubanek/cb4bca2f123446377e7e9bfefa058bf0 to your computer and use it in GitHub Desktop.
XCTAssertThrowsError(…) With Error Equality Check
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 | |
public func XCTAssertThrowsError<T, E: Error & Equatable>( | |
_ expression: @autoclosure () throws -> T, | |
expected expectedError: E, | |
_ message: String = "", | |
file: StaticString = #filePath, | |
line: UInt = #line | |
) { | |
XCTAssertThrowsError(try expression(), message, file: file, line: line) { error in | |
if let error = error as? E { | |
XCTAssertEqual(error, expectedError, file: file, line: line) | |
} else { | |
XCTFail( | |
"The type of the thrown error \(type(of: error)) does not match the type of the expected one \(type(of: expectedError)).", | |
file: file, | |
line: line | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment