Skip to content

Instantly share code, notes, and snippets.

@lukaskubanek
Created March 14, 2021 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukaskubanek/cb4bca2f123446377e7e9bfefa058bf0 to your computer and use it in GitHub Desktop.
Save lukaskubanek/cb4bca2f123446377e7e9bfefa058bf0 to your computer and use it in GitHub Desktop.
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