Instance Method

keyValueObservingExpectation(for:keyPath:handler:)

Creates an expectation that uses Key Value Observing to observe a value and respond to changes in that value by calling a provided handler.

Declaration

func keyValueObservingExpectation(for objectToObserve: Any, keyPath: String, handler: XCTKVOExpectation.Handler? = nil) -> XCTestExpectation

Parameters

objectToObserve

The object to observe.

keyPath

The key path to observe.

handler

An optional XCTKVOExpectation.Handler block. If you do not provide a handler block, the expectation will be fulfilled by the first change to the key path of the observed object.

Discussion

Creates an XCTestExpectation that uses Key Value Observing to observe changes on the value specified by keyPath on the provided object.

When changes to the value are detected, the handler block is called to assess the new value to see if the expectation has been fulfilled. Every Key Value Observing change will run the handler block until it either returns true (to fulfill the expectation), or the wait times out.

XCTAssert and related APIs can be used in the block to report a failure.

See Also

Creating Asynchronous Test Expectations

func expectation(description: String) -> XCTestExpectation

Creates a new expectation with an associated description.

func expectation(for: NSPredicate, evaluatedWith: Any?, handler: XCTNSPredicateExpectation.Handler?) -> XCTestExpectation

Creates an expectation that is fulfilled if the predicate returns true when evaluated with the given object.

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

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