<!--
{
  "availability" : [

  ],
  "documentType" : "symbol",
  "framework" : "XCTest",
  "identifier" : "/documentation/XCTest/XCTestCase/expectation(for:evaluatedWith:handler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "XCTest"
    ],
    "preciseIdentifier" : "c:objc(cs)XCTestCase(im)expectationForPredicate:evaluatedWithObject:handler:"
  },
  "title" : "expectation(for:evaluatedWith:handler:)"
}
-->

# expectation(for:evaluatedWith:handler:)

Creates an expectation that the test fulfills by evaluating the predicate with the specified object.

```
func expectation(for predicate: NSPredicate, evaluatedWith object: Any?, handler: XCTNSPredicateExpectation.Handler? = nil) -> XCTestExpectation
```

## Parameters

`predicate`

The predicate to evaluate.

`object`

The object XCTest evaluates the predicate against.

`handler`

An optional handler that performs custom evaluation when `predicate` evaluates as <doc://com.apple.documentation/documentation/Swift/true>.

## Discussion

The expectation periodically evaluates the predicate and also may use notifications or other events to optimistically re-evaluate. The test fulfills the expectation when the predicate evaluates to <doc://com.apple.documentation/documentation/Swift/true>.

When you use the resulting expectation from Swift and await using [`fulfillment(of:timeout:enforceOrder:)`](/documentation/XCTest/XCTestCase/fulfillment(of:timeout:enforceOrder:)) rather than [`wait(for:)`](/documentation/XCTest/XCTestCase/wait(for:)), XCTest evaluates `predicate` on the main actor.

If a handler isn’t provided, the first successful evaluation of the predicate fulfills the expectation. If you provide a handler, the handler can override this default behavior to tailor the conditions that fulfill the expectation.

> Note:
> For more control over predicate-based expectations, use ``doc://com.apple.xctest/documentation/XCTest/XCTNSPredicateExpectation`` instead of this convenience method.

---

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)