Last active
September 14, 2022 04:49
-
-
Save CassiusPacheco/7c5144a37ab96b2ff13e697f9927da30 to your computer and use it in GitHub Desktop.
Helper for easily asserting an async method threw an error
This file contains hidden or 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 Foundation | |
import XCTest | |
extension XCTestCase { | |
/// Expects that the async code will throw | |
@discardableResult | |
func XCTAssertThrowsErrorAsync( | |
testName: String = #function, | |
file: StaticString = #filePath, | |
line: UInt = #line, | |
test: () async throws -> Void | |
) async -> Error? { | |
do { | |
try await test() | |
XCTFail("\(testName) should have failed", file: file, line: line) | |
return nil | |
} catch { | |
// Success | |
return error | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example: