Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//: 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
@derekbassett

This comment has been minimized.

Copy link

commented Oct 30, 2015

This is so awesome!!! Thank you so much.

@dedeexe

This comment has been minimized.

Copy link

commented Jan 6, 2016

Very Interesting... A beautiful solution.... \o/

puremagic #puramagia

@amleszk

This comment has been minimized.

Copy link

commented Jan 25, 2016

How would you mock out the return value of UIUserNotificationSettings in function func registerUserNotificationSettings(notificationSettings: UIUserNotificationSettings)

  • using another protocol here would change the method signature
  • say you were unable to create this object in your tests, because it's creation requires other dependencies or third party frameworks
  • so protocol'ing the UIUserNotificationSettings is not possible
@cleexiang

This comment has been minimized.

Copy link

commented Oct 27, 2016

Use this way, should I add a protocol for every system api? I afraid that too much code will be increased.

@paramadharmika

This comment has been minimized.

Copy link

commented Sep 18, 2017

@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
You can’t perform that action at this time.