---
title: "keyValueObservingExpectation(for:keyPath:expectedValue:)"
framework: xctest
role: symbol
role_heading: Instance Method
path: "xctest/xctestcase/keyvalueobservingexpectation(for:keypath:expectedvalue:)"
---

# keyValueObservingExpectation(for:keyPath:expectedValue:)

Creates an expectation that uses Key-Value Observing to observe a value until it matches an expected value.

## Declaration

```swift
func keyValueObservingExpectation(for objectToObserve: Any, keyPath: String, expectedValue: Any?) -> XCTestExpectation
```

## Parameters

- `objectToObserve`: The object to observe.
- `keyPath`: The key path to observe.
- `expectedValue`: Expected value of the value specified by keyPath. The expectation will fulfill itself when the value at keyPath is equal to expectedValue, as tested using isEqual:. If expectedValue is nil, the expectation will be fulfilled by the first change to the key path of the observed object.

## Return Value

Return Value Creates and returns an expectation associated with the test case.

## Discussion

Discussion Creates an XCTestExpectation that uses Key Value Observing to observe changes on the provided object until the value specified by keyPath matches the expected value using isEqual:. note: For more control over KVO-based expectations, use XCTKVOExpectation 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:handler:)](xctest/xctestcase/expectation(fornotification:object:handler:).md)
- [expectation(forNotification:object:notificationCenter:handler:)](xctest/xctestcase/expectation(fornotification:object:notificationcenter:handler:).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)
