<!--
{
  "availability" : [

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

# expectation(forNotification:object:handler:)

Creates an expectation that the test fulfills when it receives a specific notification for a specified object.

```
func expectation(forNotification notificationName: NSNotification.Name, object objectToObserve: Any?, handler: XCTNSNotificationExpectation.Handler? = nil) -> XCTestExpectation
```

## Parameters

`notificationName`

The notification to register for.

`objectToObserve`

The object to observe.

`handler`

An optional [`XCTNSNotificationExpectation.Handler`](/documentation/XCTest/XCTNSNotificationExpectation/Handler-swift.typealias) block. If no handler is provided, the first notification matching the specified name from the observed object fulfills the expectation.

## Discussion

> Note:
> For more control over notification-based expectations, use ``doc://com.apple.xctest/documentation/XCTest/XCTNSNotificationExpectation`` 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)