tezvyn:

Testing async code and completion handlers in XCTest

Source: interviewintermediate

WHAT IT TESTS: knowledge of waiting for asynchronous work in tests. OUTLINE: create an expectation, fulfill it inside the completion handler, call wait with a timeout, or use async test methods.

WHAT IT TESTS: whether you can deterministically wait for asynchronous results instead of racing the callback. ANSWER OUTLINE: an XCTestExpectation represents a future event; you create it, call the function under test, fulfill the expectation inside the completion handler after asserting, then call wait with a timeout so the test pauses until fulfillment or failure. Modern Swift lets you mark the test async and simply await the call.

Read the original → interview

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

Testing async code and completion handlers in XCTest · Tezvyn