assert
A dependency for handling assertions.
var assert: any AssertionEffect { get }
Useful as a controllable and testable substitute for Swift’s assert
function that calls reportIssue
in tests instead of terminating the executable.
func operate(_ n: Int) {
@Dependency(\.assert) var assert
assert(n > 0, "Number must be greater than zero")
// ...
}
Tests can assert against this precondition using XCTExpectFailure
:
XCTExpectFailure {
operate(n)
} issueMatcher: {
$0.compactDescription = "Number must be greater than zero"
}
Dependency values
protocol AssertionEffect
A type for creating an assertion or precondition.
protocol AssertionFailureEffect
Custom assertions
AnyAssertionEffect