-
Notifications
You must be signed in to change notification settings - Fork 1
Testing and Mocking
Husnain Ali edited this page Sep 27, 2026
·
2 revisions
Test doubles ship in the library. Nothing touches the network in a unit test.
import Testing
@testable import SwiftNetworkKit
@Test func decodesTheProfile() async throws {
let transport = MockNetworkTransport()
.enqueueJSON(User(id: 1, name: "Ada"), status: 200)
let client = NetworkClient(
configuration: NetworkConfiguration(baseURL: "https://example.com"),
transport: transport
)
let user = try await client.request(GetProfile())
#expect(user.name == "Ada")
}let transport = MockNetworkTransport()
.enqueue(.status(500)) // then...
.enqueue(.status(429, headers: ["Retry-After": "2"]))
.enqueue(.error(URLError(.notConnectedToInternet)))
.enqueueJSON(order, status: 201)
// order-independent matchers instead of a FIFO queue:
let matched = MockNetworkTransport()
.stub(method: .get, pathContains: "/me", with: .json(user))
.stub(method: .post, pathContains: "/orders", with: .status(201))let client = NetworkClient(
configuration: config,
transport: MockScenario.tokenExpired(body: userJSON).transport()
)
// .happyPath / .tokenExpired / .offline / .rateLimited / .serverErrors| Double | Use |
|---|---|
URLProtocolStub |
Exercise the real URLSessionTransport wiring against canned responses |
TestClock |
Drive retry / backoff / proactive-refresh timing with advance(by:), no real waiting |
CapturingLogger |
Assert on emitted log lines (and that tokens are absent) |
MockNetworkMonitor |
Push .satisfied / .unsatisfied into reachability streams |
InMemoryOfflineStore |
Offline-queue tests without touching disk |
Inject the NetworkClient (or a protocol you define over it) into your repositories, and swap the
transport in tests. The SwiftUI demo (Examples/SwiftUIDemo/) shows an AppContainer wiring
pattern, plus a Diagnostics screen with two narrated mock-transport walkthroughs: 401 → refresh →
retry, and an account-switch that demonstrates cache isolation and clearCache().
SwiftNetworkKit is source-available proprietary software (not open source). See FAQ Licensing and Support. Repo: https://github.com/ihusnainalii/SwiftNetworkKit · Docs: https://swiftnetworkkit.vercel.app/
Getting started
Guides
- Defining Endpoints
- Authentication and Token Refresh
- Security and Certificate Pinning
- Retry Policy
- Interceptors
- Request Management
- Caching
- Offline Request Queue
- Uploads Downloads and Progress
- Pagination and Batch
- Combine and SwiftUI
- Error Handling
- Observability
- Testing and Mocking
- Best Practices and a Complete Example
- Platform Notes, Thread Safety and Performance
Reference