https://github.com/easypackages/easymock
π Lightweight Swift mocking library with async/await, delays, and error simulation
https://github.com/easypackages/easymock
easypackages mock spm stub swift testing
Last synced: about 1 year ago
JSON representation
π Lightweight Swift mocking library with async/await, delays, and error simulation
- Host: GitHub
- URL: https://github.com/easypackages/easymock
- Owner: EasyPackages
- Created: 2025-05-12T19:18:18.000Z (about 1 year ago)
- Default Branch: develop
- Last Pushed: 2025-05-16T12:44:37.000Z (about 1 year ago)
- Last Synced: 2025-06-01T01:25:23.108Z (about 1 year ago)
- Topics: easypackages, mock, spm, stub, swift, testing
- Language: Swift
- Homepage:
- Size: 158 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README

[](https://github.com/EasyPackages/EasyMock/actions/workflows/swift.yml)
# Simulate. Test. Verify
A lightweight and expressive library for unit testing in Swift β supporting `async/await`, delays, error simulation, and call tracking.
## Overview
**EasyMock** is a test utility designed for creating mock objects (test doubles) in Swift with minimal setup and maximum readability.
Itβs ideal for testing interactions, async flows, and error handling β without boilerplate.
### β¨ Features
- β
Controlled input/output (stubbing)
- π Call tracking (spies)
- β± Simulated delays (like network latency)
- π Full `async/await` support
- β Error simulation (`throw`)
- π§ͺ Designed for clarity in unit tests
## Why Use EasyMock?
### Replace This:
```swift
final class AuthenticatorMock: Authenticator {
private(set) var authenticateCallCount = 0
private(set) var wasCalledWithCredential: Credential?
var credentialDelay: Double?
var authenticateStub = makeAnyAuthenticated()
var authenticateErrorStub: Error?
func authenticate(_ credential: Credential) async throws -> Authenticated {
if let delay {
try await Task.sleep(nanoseconds: UInt64(delay * 1_000_000_000))
}
if let authenticateErrorStub {
throw authenticateErrorStub
}
authenticateCallCount += 1
wasCalledWithCredential = credential
return authenticateStub
}
}
```
### With This:
```swift
struct AuthenticatorMock: Authenticator {
let authenticateMocked = AsyncThrowableMock(makeAnyAuthenticated())
func authenticate(_ credential: Credential) async throws -> Authenticated {
try await authenticateMocked.synchronize(credential)
}
}
```
## Installation
### Using Swift Package Manager
Simply add a package to your project passing in https://github.com/EasyPackages/EasySymbol.
In your dependency you can add this in your Package.swift:
```swift
dependencies: [
.package(
url: "https://github.com/EasyPackages/EasyMock.git",
from: "1.0.0"
)
]
```
In your target:
```swift
.target(
name: "YourApp",
dependencies: ["EasyMock"]
)
```
## Examples
### Basic Mock
```swift
let mock = Mock(true)
let result = mock.synchronize("input")
#expect(mock.spies == ["input"])
#expect(result == true)
```
### AsyncMock
```swift
let asyncMock = AsyncMock("Done")
asyncMock.mock(delay: 1.0)
let result = await asyncMock.synchronize()
#expect(result == "Done")
```
### ThrowableMock
```swift
enum LoginError: Error { case invalid }
let mock = ThrowableMock(false)
mock.mock(throwing: LoginError.invalid)
XCTAssertThrowsError(try mock.synchronize("admin"))
```
### AsyncThrowableMock
```swift
let asyncMock = AsyncThrowableMock(42)
asyncMock.mock(delay: 0.5)
asyncMock.mock(throwing: nil)
let value = try await asyncMock.synchronize("ping")
XCTAssertEqual(value, 42)
```
## Supported Platforms
- iOS 13+
- macOS 10.15+
- Swift 5.9+
## Author
Created by [Paolo Prodossimo Lopes](https://github.com/PaoloProdossimoLopes)
Feel free to contribute, open issues, or suggest improvements.