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