assert

A dependency for handling assertions.

Assert.swift:24DependencyValuesAssert.md
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

Custom assertions

  • AnyAssertionEffect