require(throws:_:sourceLocation:performing:)
Declaration
@discardableResult @freestanding(expression) macro require<E, R>(throws error: E, _ comment: @autoclosure () -> Comment? = nil, sourceLocation: SourceLocation = #_sourceLocation, performing expression: () async throws -> R) -> E where E : Equatable, E : ErrorReturn Value
The instance of E that was thrown by expression and is equal to error.
Overview
Use this overload of #require() when the expression expression should throw a specific error:
try #require(throws: EngineFailureError.batteryDied) {
FoodTruck.shared.engine.batteryLevel = 0
try FoodTruck.shared.engine.start()
}If expression does not throw an error, or if it throws an error that is not equal to error, an Issue is recorded for the test that is running in the current task and an instance of ExpectationFailedError is thrown. Any value returned by expression is discarded.
If the thrown error need only be an instance of a particular type, use require(throws:_:sourceLocation:performing:) instead.