---
title: "expectation(forNotification:object:handler:)"
framework: xctest
role: symbol
role_heading: Instance Method
path: "xctest/xctestcase/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.

## Declaration

```swift
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 doc://com.apple.xctest/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

Discussion note: For more control over notification-based expectations, use XCTNSNotificationExpectation instead of this convenience method.

## See Also

### Creating Asynchronous Test Expectations

- [expectation(description:)](xctest/xctestcase/expectation(description:).md)
- [expectation(for:evaluatedWith:handler:)](xctest/xctestcase/expectation(for:evaluatedwith:handler:).md)
- [expectation(forNotification:object:notificationCenter:handler:)](xctest/xctestcase/expectation(fornotification:object:notificationcenter:handler:).md)
- [keyValueObservingExpectation(for:keyPath:expectedValue:)](xctest/xctestcase/keyvalueobservingexpectation(for:keypath:expectedvalue:).md)
- [expectation(that:on:options:willEqual:)](xctest/xctestcase/expectation(that:on:options:willequal:).md)
- [keyValueObservingExpectation(for:keyPath:handler:)](xctest/xctestcase/keyvalueobservingexpectation(for:keypath:handler:).md)
- [expectation(that:on:options:willSatisfy:)](xctest/xctestcase/expectation(that:on:options:willsatisfy:)-6itb.md)
- [expectation(that:on:options:willSatisfy:)](xctest/xctestcase/expectation(that:on:options:willsatisfy:)-292oj.md)
- [expectation(that:on:options:willSatisfy:)](xctest/xctestcase/expectation(that:on:options:willsatisfy:)-85or0.md)
