Contents

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 Handler Swift.typealias block. If you don’t provide a handler block, the first change to the key path of the observed object fulfills the expectation.

Discussion

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

When the tests detects changes to the value, it calls the handler block to assess the new value to see if the change fulfills the expectation. Every key-value observing change runs the handler block until it either returns true (to fulfill the expectation), or the wait times out.

You can use XCTAssert and related APIs in the block to report a failure.

See Also

Creating Asynchronous Test Expectations