<!--
{
  "availability" : [
    "Swift: 6.1.0 -",
    "Xcode: 16.3.0 -",
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: -",
    "macOS: -",
    "swift: 6.0.0 -",
    "tvOS: -",
    "visionOS: -",
    "watchOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "Testing",
  "identifier" : "/documentation/Testing/confirmation(_:expectedCount:isolation:sourceLocation:_:)-l3il",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Swift Testing"
    ],
    "preciseIdentifier" : "s:7Testing12confirmation_13expectedCount9isolation14sourceLocation_xAA7CommentVSg_q_ScA_pSgYiAA06SourceG0VxAA12ConfirmationVYaKYTXEtYaKSXR_s8SendableR_STR_Si5BoundSXRt_Si7ElementSTRt_r0_lF"
  },
  "title" : "confirmation(_:expectedCount:isolation:sourceLocation:_:)"
}
-->

# confirmation(_:expectedCount:isolation:sourceLocation:_:)

Confirm that some event occurs during the invocation of a function.

```
func confirmation<R>(_ comment: Comment? = nil, expectedCount: some RangeExpression<Int> & Sendable & Sequence<Int>, isolation: isolated (any Actor)? = #isolation, sourceLocation: SourceLocation = #_sourceLocation, _ body: (Confirmation) async throws -> sending R) async rethrows -> R
```

## Parameters

`comment`

An optional comment to apply to any issues generated by this
function.

`expectedCount`

A range of integers indicating the number of times the
expected event should occur when `body` is invoked.

`isolation`

The actor to which `body` is isolated, if any.

`sourceLocation`

The source location to which any recorded issues should
be attributed.

`body`

The function to invoke.

## Return Value

Whatever is returned by `body`.

## Discussion> Throws: Whatever is thrown by `body`.

Use confirmations to check that an event occurs while a test is running in
complex scenarios where `#expect()` and `#require()` are insufficient. For
example, a confirmation may be useful when an expected event occurs:

- In a context that cannot be awaited by the calling function such as an
  event handler or delegate callback;
- More than once, or never; or
- As a callback that is invoked as part of a larger operation.

To use a confirmation, pass a closure containing the work to be performed.
The testing library will then pass an instance of [`Confirmation`](/documentation/Testing/Confirmation) to the
closure. Every time the event in question occurs, the closure should call
the confirmation:

```swift
let minBuns = 5
let maxBuns = 10
await confirmation(
  "Baked between \(minBuns) and \(maxBuns) buns",
  expectedCount: minBuns ... maxBuns
) { bunBaked in
  foodTruck.eventHandler = { event in
    if event == .baked(.cinnamonBun) {
      bunBaked()
    }
  }
  await foodTruck.bakeTray(of: .cinnamonBun)
}
```

When the closure returns, the testing library checks if the confirmation’s
preconditions have been met, and records an issue if they have not.

If an exact count is expected, use
[`confirmation(_:expectedCount:isolation:sourceLocation:_:)`](/documentation/Testing/confirmation(_:expectedCount:isolation:sourceLocation:_:)-5mqz2) instead.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)