Skip to content

Instantly share code, notes, and snippets.

@hossamghareeb
Created January 6, 2021 14:51
Show Gist options
  • Save hossamghareeb/e6044dd1f623816600f48dc17b6a45e7 to your computer and use it in GitHub Desktop.
Save hossamghareeb/e6044dd1f623816600f48dc17b6a45e7 to your computer and use it in GitHub Desktop.
Singletons backdoor for unit testing
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