Skip to content
Understudy Get started

Examples

Real tests, start to finish

Each example is a test from Understudy's own suite, which builds and passes in CI. Read the short story of what it checks, then copy the test.

UnderstudyHTTPNetwork Three requests that get a 401 share one refresh Hold the token refresh, let three 401s arrive, then check each request retried with the new token. let heldRefresh = tokenRoute.holdNextCall() UnderstudyHTTPNetwork A server error reaches the client as its own error A 503 from /me becomes APIError.serviceUnavailable in the app. server.route(.get, "/me").willReturn(.status(503)) UnderstudySpies A spy keeps every call, in order Call the double twice, then read what it was called with. #expect(catalog.productSpy.calls == expectedIDs) UnderstudySpies One answer per argument A lamp for id 1, a chair for anything over 100, nil for the rest. catalog.productSpy.willReturn(.lamp, for: 1) UnderstudySpies Save before sending Watch two doubles with one CallOrder and check the order the code called them in. #expect(order.names == expectedOrder) UnderstudySpies Checkout is tracked, with two views Give the analytics spy expectations and the test fails on a missing or extra event. analytics.trackSpy.expect("checkout") UnderstudyClock Greets by name once the clock passes the delay A 400 ms sleep passes in no real time with TestClock. await clock.advance(by: .milliseconds(400)) UnderstudySamplesSamples A free product in one line The test names the price and its tags, and nothing else. let free = Product.sample(price: 0, tags: tags)