Check_Error
Check Error
XCTAssertThrowsError
XCTAssert(error)
class Translator {
func translate(_ text: String) throws -> String {
...
}
}
// Usage
func testFunc() {
// Capture the thrown error using a closure
XCTAssertThrowsError(try translator.translate("")) {
thrownError = $0
}
// First we’ll verify that the error is of the right
// type, to make debugging easier in case of failures.
XCTAssertTrue(
thrownError is Translator.Error,
"Unexpected error type: \(type(of: thrownError))"
)
// Verify that our error is equal to what we expect
XCTAssertEqual(thrownError as? Translator.Error, .emptyText)
}
Testing error code path in swift | swift by sundell