A minimal method stub for Swift.
import Stub
final class StubUserService: UserServiceProtocol {
@StubProxy(follow) var followStub
func follow(userID: Int) -> String {
followStub.invoke(userID, default: "")
}
@StubProxy(edit) var editStub
func edit(userID: Int, name: String) -> Bool {
editStub.invoke((userID, name), default: false)
}
}
func testMethodCall() {
// given
let userService = StubUserService()
userService.followStub.register { userID in "stub-\(userID)" } // stub
// when
userService.follow(userID: 123) // call
// then
let executions = userService.followStub.executions()
XCTAssertEqual(executions.count, 1)
XCTAssertEqual(executions[0].arguments, 123)
XCTAssertEqual(executions[0].result, "stub-123")
}
@StubProxy(fetchOverview) var fetchOverviewStub
func fetchOverview(id: String) -> Overview {
fetchOverviewStub.invoke(id, default: Overview.fixture)
}
⚠️ You need to register a stub before callinginvokeWithoutDefault()
@StubProxy(fetchOverview) var fetchOverviewStub
func fetchOverview(id: String) -> Overview {
fetchOverviewStub.invokeWithoutDefault(id)
}
let convertStubStore = StubStore()
func convert<Source, Target>(_ source: Source) -> Target {
let stub = convertStubStore.stub(arguments: Source.self, result: Target.self)
return stub.invokeWithoutDefault(value)
}
Stub.clearAll()
Stub is under MIT license. See the LICENSE for more info.