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.
Swift 6 · Apple platforms and Linux · 1.3.0
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
Four tools for the parts of a test that aren't your code. Each has a guide.
@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.
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.
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.
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.
StubServer gives a real URLSession scripted routes. Tests run in parallel, and a credential never reaches a report.A real URLSession with scripted routes.
@Sample writes sample(...) for a struct, so a test passes only the values it cares about.@Sample writes sample(...) for a struct.
Products
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
@GenerateSpies writes a spy for each function and property of your protocol. TestClock moves only when your test moves it.
Spies and the clockimport UnderstudyHTTP
StubServer gives a test a real URLSession that talks to scripted routes, with no network. Each route is a spy.
Stub the networkimport UnderstudySamples
@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 valuesExample
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.
Code that sleeps takes a clock. The test passes a TestClock and advances it, so a 400 ms delay passes in no real time.
StubServer answers scripted routes. Each route is a spy, so held calls, answers by condition and expectations all work on requests.
Mark a struct with @Sample and a test names only the properties it is about. Everything else gets a sensible default.
More examples@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)
Add the package, mark a class, script one answer and check one call. The tutorial takes about ten minutes.