Last active
October 23, 2019 05:03
-
-
Save eliperkins/8f4115151497dc1953ea to your computer and use it in GitHub Desktop.
This file contains 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
//: Mocks Playground | |
import UIKit | |
struct User { | |
} | |
struct PushNotificationController { | |
let registrar: PushNotificationRegistrar | |
init(registrar: PushNotificationRegistrar) { | |
self.registrar = registrar | |
} | |
var user: User? { | |
didSet { | |
if let _ = user { | |
registrar.registerUserNotificationSettings(UIUserNotificationSettings()) | |
} | |
} | |
} | |
} | |
protocol PushNotificationRegistrar { | |
func registerUserNotificationSettings(notificationSettings: UIUserNotificationSettings) | |
} | |
extension UIApplication: PushNotificationRegistrar { } | |
class FauxRegistrar: PushNotificationRegistrar { | |
var registered = false | |
func registerUserNotificationSettings(notificationSettings: UIUserNotificationSettings) { | |
registered = true | |
} | |
} | |
var registrar = FauxRegistrar() | |
var controller = PushNotificationController(registrar: registrar) | |
controller.user = User() | |
registrar.registered |
Use this way, should I add a protocol for every system api? I afraid that too much code will be increased.
@cleexiang, you only need to test public method, in your boundary (the one that interested you much). Not every single system api need testing.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How would you mock out the return value of
UIUserNotificationSettings
in functionfunc registerUserNotificationSettings(notificationSettings: UIUserNotificationSettings)
UIUserNotificationSettings
is not possible