Created
January 6, 2021 14:51
-
-
Save hossamghareeb/e6044dd1f623816600f48dc17b6a45e7 to your computer and use it in GitHub Desktop.
Singletons backdoor for unit testing
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 Singleton { | |
private static var instance = Singleton() | |
#if DEBUG | |
static var stubbedInstance: Singleton? | |
#endif | |
static var shared: Singleton { | |
#if DEBUG | |
if let stubbedInstance = stubbedInstance { return stubbedInstance } | |
#endif | |
return instance | |
} | |
} | |
// Usage in tests | |
// Singleton.stubbedInstance = Singleton() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment