Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
XCTAssertThrowsError(…) With Error Equality Check
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