Skip to content
Understudy Get started

All examples

UnderstudySpies

One answer per argument

A stub that looks things up is one answer per argument. Script each one on the spy instead of writing a closure that looks the argument up.

Run it

swift test --filter catalogAnswersByProductID
The whole file on GitHub
ScriptAnswersByArgumentGuide.swift
@Test("A catalog answers each product id as scripted")
func catalogAnswersByProductID() {
    catalog.productSpy.willReturn(.lamp, for: 1)
    catalog.productSpy.willReturn(.chair, when: { productID in productID > 100 })

    #expect(catalog.product(id: 1) == .lamp)
    #expect(catalog.product(id: 101) == .chair)
    #expect(catalog.product(id: 2) == nil)
}