Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A guide to mocking objects with static methods...
protocol AnalyticsBackEnd {
static func log(_ name: String)
}
extension Analytics: AnalyticsBackEnd {}
class AnalyticsService {
let backEnd: AnalyticsBackEnd.Type
init(backEnd: AnalyticsBackEnd.Type = Analytics.self) {
self.backEnd = backEnd
}
func log(event: String) {
backEnd.log(event)
}
}
func testAnalyticsLogging() {
let subject = AnalyticsService(backEnd: MockAnalyticsBackEnd.self)
// Test...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment