A Swift package to add Mocking capabilities to your iOS app.
When mocking is enabled and registered, any URL will look for a corresponding JSON file by default that matches the sanitised value of the URL's absoluteString value.
Additional manual mocking can also be added to override this default behavour for specific needs.
Any URL that isn't mocked will be reported using OSLog to help idenfity areas to mock.
- Swift 5.7
- iOS 14
Add the Mocking package as a dependency to your Package.swift
file, and add it as a dependency to your target.
// swift-tools-version:5.7
import PackageDescription
let package = Package(
name: "MyPackage",
dependencies: [
.package(url: "https://github.com/pdamonkey/Mocking.git", upToNextMajor: "1.0.0")
],
targets: [
.target(name: "MyPackage", dependencies: ["Mocking"])
]
)
To use the framework, import the module:
import Mocking
Check if the --mocking
Launch Argument has been set:
if MockingURLProtocol.isMocking {
...
}
Register MockingURLProtocol with URLProtocol:
MockingURLProtocol.register()
Add MockingURLProtocol to the URL configuration if required:
configuration.protocolClasses = [MockingURLProtocol.self]
Add mock matching URL to return specific Data object and HTTP Response:
MockingURLProtocol.mock(url: url, data: data-to-return, response: response-to-return)
Add mock matching URL to return specific Data and optional Status Code (defaults to 200)
MockingURLProtocol.mock(url: url, data: data-to-return, 404)
Add mock matching URL to return specific Error and Status Code:
MockingURLProtocol.mock(url: url, error: MyError.notAuthenticated, statusCode: 401)
Use Regex to mock pretified URL's to a particular mock file:
MockingURLProtocol.mock(regex: "^my-url-thumbnail-*", data: Bundle.main.mockFile(name: "thumbnail"))
U&nregister MockingURLProtocol with URLProtocol (if required):
MockingURLProtocol.unregister()