Skip to content
Understudy Get started

Swift 6 · Apple platforms and Linux · 1.3.0

Plays the part.
Remembers every line.

Mocks and fakes for your Swift tests, written for you. Mark what your double stands in for, and Understudy's macros record every call and answer the way your test says.

.package(url: "https://github.com/joeoct-oss/Understudy", from: "1.3.0")

What it covers

Every call, from script to check

Four tools for the parts of a test that aren't your code. Each has a guide.

Spies, written for you

@GenerateSpies writes a double for your protocol: functions, properties, subscripts, async and throwing calls, generics and actors.A double for your protocol from one macro.

Answers you script

willReturn, willThrow, willGiveNext, and answers by argument with when:. Give a spy an expectation and it becomes a strict mock.willReturn, willThrow and answers by argument.

Calls you can check

Read calls, expect a count or a range, and check order across doubles with CallOrder. Failures point at the line.Counts, ranges and order across doubles.

Code that waits

Hold a call and release it when the test is ready. TestClock moves time only when the test moves it.Held calls and a clock your test moves.

A network with no network

StubServer gives a real URLSession scripted routes. Tests run in parallel, and a credential never reaches a report.A real URLSession with scripted routes.

Complete sample values

@Sample writes sample(...) for a struct, so a test passes only the values it cares about.@Sample writes sample(...) for a struct.

Products

Three products, one cast

Add only what a target needs. Spies and the clock go in test targets; samples can live next to your structs, even in an app target.

import Understudy

Understudy

Spies and the clock

@GenerateSpies writes a spy for each function and property of your protocol. TestClock moves only when your test moves it.

Spies and the clock
import UnderstudyHTTP

UnderstudyHTTP

Stub the network

StubServer gives a test a real URLSession that talks to scripted routes, with no network. Each route is a spy.

Stub the network
import UnderstudySamples

UnderstudySamples

Make sample values

@Sample writes sample(...) for a struct, so a test gets a complete value in one call and names only what it's about.

Make sample values

Example

A double is a class with marks on it

Write a class that conforms to your protocol, mark it, and leave the bodies out. The macro writes them, plus a spy for each one that records calls and gives the answers you script.

More examples
CatalogTests.swift
@GenerateSpies
final class SpyCatalog: Catalog {
    @Spied func product(id productID: Int) -> Product?
}

@Test func showsTheProductItWasOpenedWith() {
    let catalog = SpyCatalog()
    catalog.productSpy.willReturn(.apple)

    let screen = ProductScreen(id: 7, catalog: catalog)

    #expect(screen.title == "Apple")
    #expect(catalog.productSpy.calls == [7])
}
@Test("Greets by name once the clock passes the delay")
func greetsAfterTheDelay() async throws {
    let clock = TestClock()
    let greeter = Greeter(clock: clock)

    async let greeting = greeter.greet("ana")
    await clock.advance(by: .milliseconds(400))

    #expect(try await greeting == "Hello, ana")
}
@Test("A server error reaches the client as its own error")
func mapsAServerErrorToAppError() async {
    let server = StubServer()
    server.route(.get, "/me").willReturn(.status(503))
    let apiClient = APIClient(session: server.session())

    await #expect(throws: APIError.serviceUnavailable) {
        try await apiClient.currentUser()
    }
}
@Sample
struct Product {
    let id: UUID
    let name: String
    let price: Decimal
    var stock: Int = 5
}

// Only the price matters to this test.
let free = Product.sample(price: 0)

Write your first double

Add the package, mark a class, script one answer and check one call. The tutorial takes about ten minutes.