<!--
{
  "availability" : [
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: -",
    "macOS: -",
    "tvOS: -",
    "visionOS: -",
    "watchOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "XCTest",
  "identifier" : "/documentation/XCTest/XCTKVOExpectation/init(keyPath:object:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "XCTest"
    ],
    "preciseIdentifier" : "c:objc(cs)XCTKVOExpectation(im)initWithKeyPath:object:"
  },
  "title" : "init(keyPath:object:)"
}
-->

# init(keyPath:object:)

Creates an expectation that any KVO change to the specified key path of the observed object fulfills.

```
convenience init(keyPath: String, object: Any)
```

## Parameters

`keyPath`

The key path to observe.

`object`

The object to observe.

## Discussion

This initializer sets up KVO observation for `keyPath` with the following <doc://com.apple.documentation/documentation/Foundation/NSKeyValueObservingOptions>:

- <doc://com.apple.documentation/documentation/Foundation/NSKeyValueObservingOptions/new>
- <doc://com.apple.documentation/documentation/Foundation/NSKeyValueObservingOptions/old>

The inclusion of the <doc://com.apple.documentation/documentation/Foundation/NSKeyValueObservingOptions/new> and <doc://com.apple.documentation/documentation/Foundation/NSKeyValueObservingOptions/old> options means that any custom KVO handler you provide in the expectation’s [`handler`](/documentation/XCTest/XCTKVOExpectation/handler-swift.property) property receives a change info dictionary that contains the <doc://com.apple.documentation/documentation/Foundation/NSKeyValueChangeKey/newKey> and <doc://com.apple.documentation/documentation/Foundation/NSKeyValueChangeKey/oldKey> keys.

---

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)