Advanced Protocol Mock, Type Erasure and dependency-forwarding Code-generation templates for Swift (using Sourcery).
Three templates:
Mocks.swifttemplate— generates protocol mock classes for test doubles with call counting, handler closures, and smart defaultsTypeErase.swifttemplate— generates type erasure wrappersComponent.swifttemplate— generates the forwarding class that satisfies a dependency-injection protocol, for trees where each level declares what it consumes. See Components
The first two support protocols with associated types, generic functions with constraints, @escaping closure parameters,
and provide smart defaults for RxSwift and Combine types. Compatible with RIBs architecture patterns.
Generated mocks compile with zero diagnostics under -strict-concurrency=complete and in the
Swift 6 language mode: async and nonisolated are carried through to the mock, a protocol's
global actor is declared on the mock class, and a Sendable protocol gets an @unchecked
conformance. See Concurrency.
Generated Mock API
For a protocol method:
/// sourcery: ProtocolMock
protocol DataService {
func fetchData(id: String, completion: @escaping (String?, Error?) -> Void)
}
The template generates:
final class DataServiceMock: DataService {
func fetchData(id: String, completion: @escaping (String?, Error?) -> Void) {
fetchDataCallCount += 1
fetchDataArgs.append(id)
if let __fetchDataHandler = self.fetchDataHandler {
__fetchDataHandler(id, completion)
}
}
var fetchDataCallCount: Int = 0
var fetchDataArgs: [String] = []
var fetchDataHandler: ((_ id: String, _ completion: @escaping (String?, Error?) -> Void) -> ())? = nil
}
The naming rule: a mock member is the declared name plus a suffix. The name is taken verbatim,
with backticks dropped and nothing else changed — no case change, no underscore removal, no
first-word lowercasing. func perform1_0() gives perform1_0CallCount, func ID() gives
IDCallCount, func `do`() gives doCallCount, var setting4_2: Int gives setting4_2GetCount.
An overload is the one case where the prefix is not the declared name: the overload with the fewest
parameters keeps it, and every other one appends the capitalized argument label of each parameter,
or the parameter name where there is none. The generated file states the rule at its top and under
every // MARK: - <Protocol>, and every member whose prefix is not its declared name carries a
comment naming both spellings — above the witness, and in an index under that class's MARK:.
| suffix | on | emitted when |
|---|---|---|
CallCount |
method | always |
Args |
method | at least one recordable parameter |
Handler |
method | always |
GetCount, GetHandler |
property | always |
SetCount |
property | the requirement is { get set } |
SetArgs |
property | the requirement is { get set } and its type is not a closure |
SetHandler |
property | the requirement is { get set } |
_<var> |
property | the stored value, seeded and read without moving a counter — and the only way to seed a { get } requirement, whose witness is get-only |
Subject |
both | AnyPublisher, Observable, Single |
SubscribeCount, SubscribeCancelCount |
both | AnyPublisher — the code under test subscribed, and cancelled |
OutputCount, Outputs, OutputHandler |
both | AnyPublisher — values the member delivered |
CompletionCount |
both | AnyPublisher — the stream finished or failed |
EventCallCount, Events, EventHandler |
both | AnyObserver — events pushed in |
CancelCallCount, CancelHandler |
method | the return type is AnyCancellable |
DisposeCallCount, DisposeHandler |
method | the return type is Disposable |
A returned token's suffix is the call that token exposes: cancel() on AnyCancellable,
dispose() on RxSwift's Disposable. Every name is built in
templates/Mocks/MockNaming.swift, and two members of one mock
that would carry the same name fail generation naming both.
Key features:
- Call counting —
methodCallCounttracks invocation count - Argument recording —
methodArgsholds what each call was passed, in order. See Recorded arguments - Handler closures —
methodHandlerlets tests control behavior @escaping/@Sendablepreservation — closure parameters retain both attributes in the method signature and the handler type, so handlers can capture, async-dispatch, and hand off closures that cross isolation boundariesasync/throwspreservation — anasyncrequirement generates anasyncmethod with anasynchandler, so a spec can control when the call returns, not only what it returns- Smart defaults — Optional returns
nil, Void returns nothing, known types get sensible defaults, and RxSwift / Combine types get a subject the test drives - Overload disambiguation — overloaded methods get distinct handler names automatically
- Property read and write counting — every property requirement generates
<var>GetCount,<var>GetHandlerand a_<var>store, and a{ get set }one also generates<var>SetCount,<var>SetArgsand<var>SetHandler;mock._draftreads and seeds the value without moving a counter or recording a write - Effectful property requirements —
{ get async },{ get throws }and{ get async throws }generate the accessor they declare, with the same effects on<var>GetHandler
Recorded arguments
Every call appends what it was passed to <method>Args, so asserting on an argument needs no handler
and no side object:
let service = DataServiceMock() // func fetchData(id: String, completion: …)
let registry = AppServicesRegisteringMock() // func registerURLHandler(_ tag: String, priority: Int)
sut.load(id: "m1")
XCTAssertEqual(service.fetchDataArgs, ["m1"]) // one parameter: its own type
XCTAssertEqual(registry.registerURLHandlerArgs.last?.priority, 3) // two: a labelled tuple
service.fetchDataArgs = [] // clearing it is the reset
| the method's parameters | <method>Args |
|---|---|
| none | not generated |
| one recordable | [T] — Swift has no single-element labelled tuple to put it in |
| two or more recordable | [(first: A, second: B)], labelled with the parameter names, one element per call |
| a mix of recordable and closure | the recordable ones only |
inout T |
[T] — the value the caller passed in; the handler still gets the reference |
A { get set } requirement records its writes the same way. Every assignment appends the value to
<var>SetArgs: [T], whether or not <var>SetHandler is set; the store is then assigned and the
handler called, so code the handler calls back into reads the value just written. Seeding _<var>
and the generated initializer record nothing:
sut.mute()
XCTAssertEqual(player.volumeSetArgs, [0])
Three things are deliberately not recorded:
- Closures — a closure parameter, and a value written to a closure-typed property. A non-escaping closure cannot be stored at all, and storing an escaping one would keep the caller's captures alive for as long as the mock — which a leak or churn spec reads as a retain by the code under test. What a spec does with a closure is call it, and the handler hands it over. The generated file states this in a comment above that handler.
- Generic methods. Their parameter types name the method's generic parameters; a stored property can only name the class's.
- Anything annotated
skipArgumentRecording— on the method or the property, or on the protocol for all of them. A recorded argument or written value lives as long as the mock, so this is the opt-out for a value whose deallocation a spec asserts. Counting and the handlers are unaffected.
Concurrency
The generated file is held to zero diagnostics under -swift-version 5 -strict-concurrency=complete
and under -swift-version 6 — see Tests/Checks/.
| protocol declares | mock gets |
|---|---|
@MainActor (or any attribute ending in Actor) |
the same attribute on the class, so the conformance is isolated rather than inferred |
nonisolated func / nonisolated var on an isolated protocol |
nonisolated on the member and nonisolated(unsafe) on its call counter and handler — without both, the member cannot mutate its own bookkeeping |
func f() async throws -> T |
func f() async throws -> T with handler ((…) async throws -> (T))?, awaited at the call |
: Sendable |
final class …: P, @unchecked Sendable — a test double holds mutable counters, so the conformance cannot be checked |
@Sendable closure parameter |
the attribute restated in the signature and the handler type, so the captured closure can be handed to @Sendable-constrained code |
| a global actor Sourcery cannot name from the attribute | declare it: /// sourcery: globalActor = "MyIsolation" |
Two deliberate non-goals:
- Member-level global actors are not propagated. A protocol that is not isolated but whose method
is
@MainActorproduces a non-isolated mock method. That satisfies the requirement, and it keeps the mock callable from a non-isolated test body. - Mock classes are
final. Subclassing a generated mock is not supported; set a handler instead.
Conditional compilation
Sourcery reads every clause of an #if and records no condition, so a requirement declared inside
#if canImport(UIKit) is generated on every platform unless its declaration says otherwise. Say it with
sourcery: if, whose value is the text after #if:
/// sourcery: ProtocolMock
protocol AvatarRepositoryProtocol {
func fetch(id: String) -> String
#if canImport(UIKit)
// sourcery: if = "canImport(UIKit)"
func uploadOwnPhoto(_ image: UIImage)
#endif
}
| the declaration carries | generated |
|---|---|
if on a method or a property |
its members between #if <value> and #endif, in the mock, the Component and the type erasure |
if on the protocol |
the whole mock, Component or type erasure inside #if <value> |
several if lines |
the values joined with && |
// sourcery:begin: if = "<value>" … // sourcery:end |
the value on every declaration between the two lines |
the same method in two clauses, each with its own if |
one member, under the two conditions joined with || |
one property name with a different type in each clause, each with its own if |
both, each under its own condition, with the same member names |
canImport(<Module>) in an if value |
the generated file's import <Module> between #if canImport(<Module>) and #endif, added when the config lists no such import |
A property inside its own if that the mock's initializer takes — its type has no default value, or it
carries init — fails generation, because the initializer is generated once, outside the condition.
Put sourcery: handler on it, or give it a type with a default value.
Combine
An AnyPublisher<Output, Failure> requirement is backed by a subject the test drives:
/// sourcery: ProtocolMock
@MainActor
protocol UserRepositoryProtocol {
var meStream: AnyPublisher<UserSummary?, Never> { get }
func bootstrap(displayName: String?) -> AnyPublisher<Void, Error>
}
mock.meStreamSubject.send(UserSummary(uid: "u1", displayName: "Ada")) // state, replayed
mock.bootstrapSubject.send(()) // event
The member hands back a construct the mock owns, not the erased subject, so it records what the code under test did with the stream rather than only that it asked for one:
| counter | what it answers |
|---|---|
<var>GetCount / <method>CallCount |
it asked for the stream |
<name>SubscribeCount |
it subscribed |
<name>OutputCount, <name>Outputs |
a value reached it — counted on delivery, so a value sent while nobody is subscribed counts nothing and one delivered to two subscribers counts twice |
<name>CompletionCount |
the stream finished or failed |
<name>SubscribeCancelCount |
it cancelled |
<name>OutputHandler runs after the counter and the recorder and sees each value as it lands, which
is how a test sends the next one from inside it. /// sourcery: skipArgumentRecording on the member
or the protocol drops <name>Outputs, leaving the counters and the handler.
A property's <var>GetHandler is read when the code under test subscribes, not when it reads the
member, so a handler seeded after the publisher was captured still decides the stream. A method's
<method>Handler stays at call time, where its arguments are and where its async and throws
apply, and a method handler that answers bypasses the subject — <method>SubscribeCount then stays
at zero. A publisher requirement declared { get async } or { get throws } fails generation: the
closure that reads its handler is synchronous.
The subject is a PassthroughSubject, for a variable and for a method alike — the same rule the
RxSwift members follow with PublishSubject. The double emits what the test sends it and nothing
else, so a subscriber that attaches after a send has missed it.
A stream that replays is the test's to supply, through the closure every publisher member already
has — <name>GetHandler on a variable, <name>Handler on a method:
let state = CurrentValueSubject<UserSummary?, Never>(me)
mock.meStreamGetHandler = { state.eraseToAnyPublisher() } // now it replays
Seeding by default instead would make the double emit a value nobody wrote — "", [:], [] — the
moment the code under test subscribes, and turn the test's own send into a second element; a
bridge awaiting the first value then resumes its continuation twice and traps. It is also
un-opt-out-able by construction, since assigning a PassthroughSubject to a
CurrentValueSubject-typed property does not compile.
/// sourcery: subject = "CurrentValue" states the seeded form at the declaration, for the member
where every test wants it; asking for it where Output has no default value is an error, not a
silent downgrade.
A method returning AnyCancellable gets a token whose cancel() is counted —
<method>CancelCallCount and <method>CancelHandler — mirroring the RxSwift Disposable case, so a
registration API's deregistration is observable without setting a handler.
Components
In a dependency-injection tree where every level declares an <X>Dependency protocol naming exactly
what it consumes, the class that satisfies that protocol by forwarding to the parent is mechanical:
one var m: T { dependency.m } per requirement. Component.swifttemplate writes it.
/// sourcery: DuetComponent
protocol TimelineDependency: AnyObject {
var themeProvider: ThemeProviding { get }
var memoryRepository: MemoryRepositoryProtocol { get }
}
final class TimelineComponent: TimelineDependency {
private let dependency: TimelineDependency
init(dependency: TimelineDependency) {
self.dependency = dependency
}
var memoryRepository: MemoryRepositoryProtocol { dependency.memoryRepository }
var themeProvider: ThemeProviding { dependency.themeProvider }
}
The parent satisfies a child's Dependency with an empty extension when its own surface covers it
(extension MainComponent: TimelineDependency {}), so the tree is written once, in the protocols.
A level that owns something annotates DuetComponent, owns. A generated type cannot carry a
hand-written lazy var, so the emission becomes a non-final <X>ComponentBase and the level
writes the subclass that holds what it owns:
/// sourcery: DuetComponent, owns
protocol MainDependency: AnyObject { /* … */ }
final class MainComponent: MainComponentBase {
lazy var feedAudioPlayer: FeedAudioPlaying = FeedAudioPlayer(
memoryRepository: memoryRepository) // reads the generated forwarders
}
Getting owns wrong is a compile error, not a silent defect: a final class cannot be subclassed,
and a stored property cannot be added in an extension.
What it forwards. Read-only and settable requirements, async / throws / rethrows methods,
effectful property requirements ({ get async throws }), inout and unlabelled parameters, generic
methods, @escaping / @Sendable closure parameters, and requirements inherited from a refined
protocol. Isolation follows the same rules as the mocks: the protocol's global actor lands on the
class, a nonisolated requirement is restated, and the storage becomes nonisolated(unsafe) where a
nonisolated forwarder has to read it.
What it refuses, each with a diagnostic naming the member: static requirements, init
requirements, subscript requirements, and associated types. None can be discharged by forwarding to
a stored instance, so generation fails rather than emitting a class that will not conform.
A requirement or a protocol carrying sourcery: if is forwarded inside the same #if
(Conditional compilation).
The emitted type is internal by default, even when the protocol is public — a Component is
consumed by its own module's builders, and widening a module's API surface as a side effect of
generating boilerplate is not a decision a template should make. componentAccess = "public" opts in.
Mocks and Components compose: a protocol annotated /// sourcery: ProtocolMock, DuetComponent gets the
double a spec drives and the production class that forwards. Both read the same isolation rules from
the same helpers, so they cannot disagree about which member is nonisolated.
Tests
| lane | command | covers |
|---|---|---|
| fast | Tests/Checks/run-checks.sh |
snapshot of every template's generated output, both language modes, runtime behaviour. Mocks and Components are typechecked together, so the two cannot disagree about isolation. No simulator, no third-party packages, seconds |
| plugin | Tests/Checks/run-plugin-checks.sh |
the SPM build-tool plugin, black-box over a fixture package: the derived source closure, the synthesized config, the defaults it supplies, and five red controls that must stay red. No simulator |
| xcode | Tests/Checks/run-xcode-checks.sh |
the same plugin through XcodeBuildToolPlugin, over an Xcode project XcodeGen generates from a committed spec: config discovery, the one-directory expansion, a hand-listed ${SOURCERY_PROJECT} entry, a target depending on a sibling target, and a bare template name that must fail. Needs xcodegen, no simulator |
| full | Tests/Examples/ExampleProjectSpm/test-ios.sh |
RxSwift smart defaults, RIBs external annotation, type erasure, the SPM plugin. Needs an iOS Simulator |
All of them run on every push (.github/workflows/ci.yml), the fast lane
first. Run them locally before cutting a tag.
Release notes, including what each version changes in the generated output and what breaks: CHANGELOG.md. Working on the templates themselves — layout, where to change what, the release procedure: [CONTRIBUTING.md](https://githu
