Hi, Swift community
I'm excited to share GitHub - DanielCardonaRojas/swift-mocking: A compact swift mocking library powered by macros and parameter packs. , a modern mocking library with a unique take.
What Makes It Different?
At its core, SwiftMocking uses parameter packs to preserve exact function signatures across any arity. A Spy<String, Int, AsyncThrows, User> represents (String, Int) async throws -> User with perfect type fidelity. Spy is callAsFunction to provide a natural, function-like syntax.
Effect types (async, throws) thread through as phantom types, ensuring the compiler enforces correctness.
The @Mockable macro generates remarkably compact protocol conformances (see examples), so even writing by hand can be an option.
The library also exposes a base class Mock (used by the macro), that manages Spy creation for every protocol requirement automatically (using @dynamicMemberLookup).
Key Features
- Comprehensive protocol support: Associated types, generics, static methods, subscripts, variadic parameters, callbacks
- An extensible
ArgMatcherwith a many out of the box matchers to choose from, andExpressibleByLiteralconformances for better ergonomics. - Mockito-style API: Familiar
when()/verify()syntax with sophisticated argument matching using Swift's literal protocols and range syntax - Type-safe dynamic stubbing: Preserves concrete parameter types in closures—no casting to
Anyrequired - Dual framework integration: Supports for both
XCTestandTesting - Concurrent test isolation: Uses
TaskLocalvalues for safe parallel test execution - Test-scoped defaults: Suite and test-level default values via traits (.withDefaults)
- Closure-based testing: Test dependencies directly using Spy instances without requiring protocols
- Cross-mock call verification: Verify interaction order across multiple mock objects with
verifyInOrder() - Descriptive error messages: Clear, actionable feedback when assertions fail.
I'd love to hear feedback from the community!