Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to write the Unit Test for static functions?
import Foundation
import AppTrackingTransparency
import PlaygroundSupport
protocol PrivacyManagerBridge {
static func requestTrackingAuthorization(completionHandler completion: @escaping (ATTrackingManager.AuthorizationStatus) -> Void)
}
extension ATTrackingManager: PrivacyManagerBridge {}
protocol AppPrivacyManagerContract {
func requestTrackingAuthorization(completionHandler completion: @escaping (ATTrackingManager.AuthorizationStatus) -> Void)
}
class AppPrivacyManager: AppPrivacyManagerContract {
private var trackable: PrivacyManagerBridge.Type!
init(trackable: PrivacyManagerBridge.Type = ATTrackingManager.self) {
self.trackable = trackable
}
func requestTrackingAuthorization(completionHandler completion: @escaping (ATTrackingManager.AuthorizationStatus) -> Void) {
self.trackable.requestTrackingAuthorization(completionHandler: completion)
}
}
class ATTrackingManagerMock: PrivacyManagerBridge {
static func requestTrackingAuthorization(completionHandler completion: @escaping (ATTrackingManager.AuthorizationStatus) -> Void) {
// Return whichever flow you want to test.
completion(.denied)
}
}
let privacyManager = AppPrivacyManager(trackable: ATTrackingManagerMock.self)
privacyManager.requestTrackingAuthorization { result in
// result here
}
PlaygroundPage.current.finishExecution()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment