Created
December 18, 2018 21:17
-
-
Save JoshuaSullivan/ec730a453f0a1c24642cbd014229aee5 to your computer and use it in GitHub Desktop.
A guide to mocking objects with static methods...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protocol AnalyticsBackEnd { | |
| static func log(_ name: String) | |
| } | |
| extension Analytics: AnalyticsBackEnd {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class AnalyticsService { | |
| let backEnd: AnalyticsBackEnd.Type | |
| init(backEnd: AnalyticsBackEnd.Type = Analytics.self) { | |
| self.backEnd = backEnd | |
| } | |
| func log(event: String) { | |
| backEnd.log(event) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
Hi! Are you tried with Swift 5/6?
It's not working for me. Would you know how to make it work?