Skip to content
Understudy Get started

All examples

UnderstudySpies

A spy keeps every call, in order

A spy records each call's arguments, so the test reads what the code asked for instead of guessing from the result.

Run it

swift test --filter spyKeepsEveryCall
The whole file on GitHub
ScriptASpyAndReadItsCallsGuide.swift
@Test("A spy keeps every call, in order")
func spyKeepsEveryCall() {
    let catalog = SpyCatalog()

    _ = catalog.product(id: 7)
    _ = catalog.product(id: 9)

    let expectedIDs = [7, 9]
    #expect(catalog.productSpy.calls == expectedIDs)
    #expect(catalog.productSpy.callCount == 2)
    #expect(catalog.productSpy.lastCall == 9)
    #expect(catalog.productSpy.wasCalled)
}