Easy function mocking/stubbing in Swift
Efs
Efs, as the pronounced plural of letter F (for function), is a simple mocking/stubing library for testing purpose.
Kind of functions
✅ Passthrough functions
You can stub functions to just return simple values : in Efs, it’s called Passthrough.
// Based on this kind of injection:
struct Dependencies {
typealias SaveToDisk = (User) -> User.SaveStatus
let saveToDisk: SaveToDisk
init(saveToDisk: @escaping SaveToDisk) {
self.saveToDisk = saveToDisk
}
}
// Inject a simple passthrough stub:
let dependencies = Dependencies(
saveToDisk: Efs.passthrough(.succeed)
)
dependencies.saveToDisk(user) // returns .succeed
❌ Failing functions
You can mock functions with automatic failing. It can alert you when some functions are called without intention of.
// Inject a simple failing stub:
let dependencies = Dependencies(
saveToDisk: Efs.failing()
)
dependencies.saveToDisk(user) // will raise fatalError