Stubbles is a declarative HTTP stubbing library written in Swift, with support for iOS, tvOS and macOS.
CodeKit is available via the Swift Package Manager which is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system and automates the process of downloading, compiling, and linking dependencies.
Once you have your Swift package set up, adding Stubbles as a dependency is as easy as adding it to the dependencies value of your Package.swift.
dependencies: [
.package(
url: "https://github.com/aplr/Stubbles.git",
.upToNextMajor(from: "0.0.1")
)
]
class ApiTest: XCTestCase {
override func setUp() {
Stubbles.shared.start()
super.setUp()
}
override func tearDown() {
Stubbles.shared.stop()
super.tearDown()
}
func testGetTodo() async throws {
// Arrange
let todoApi = TodoApi()
let expectedTodo = Todo(id: 1, userId: 1, title: "todo", completed: false)
let stub = Stubbles.shared.stub {
Endpoint("https://jsonplaceholder.typicode.com/todos/1")
StubResponse {
StatusCode(201)
JsonBody(expectedTodo)
}
}
// Act
let actualTodo = try await todoApi.getTodo(id: 1)
// Assert
XCTAssertEqual(expectedTodo, actualTodo)
XCTAssertEqual(stub.calls.count, 1, "Expected request to be sent exactly once.")
}
}
Documentation is available here and provides a comprehensive documentation of the library's public interface.
Stubbles is licensed under the MIT License.