Skip to content

Instantly share code, notes, and snippets.

@cipolleschi
Created April 18, 2020 12:11
Show Gist options
  • Select an option

  • Save cipolleschi/b329cacb2a3a3e122212fe49b8a9f174 to your computer and use it in GitHub Desktop.

Select an option

Save cipolleschi/b329cacb2a3a3e122212fe49b8a9f174 to your computer and use it in GitHub Desktop.
import XCTest
class MockedNotificationScheduler: NotificationScheduler {
struct Notification: Equatable {
let title: String
}
var scheduledNotifications: [Notification] = []
func scheduleNotification(with title: String) {
self.scheduledNotifications.append(Notification(title: title))
}
}
class TimeProviderStub: TimeProvider {
let now: Date
let calendarClosure: (Calendar.Identifier) -> Calendar
func calendar(with identifier: Calendar.Identifier) -> Calendar {
return calendarClosure(identifier)
}
}
class TestNotificationManager: XCTestCase {
var notificationScheduler: MockedNotificationScheduler!
var timeProvider: TimeProvider!
func setUp() {
super.setUp()
self.notificationScheduler = MockedNotificationScheduler()
}
func testMorningNotification_before11_notificationIsScheduled() {
let timeProvider = TimeProviderStub(
date: DateComponents(hour: 10).date!,
calendarClosure: { id in return Calendar(identifier: id))
}
let notificationManager = NotificationManager(
notificationScheduler: self.notificationScheduler,
timeProvider: timeProvider
)
notificationManager.scheduleMorningNotification()
XCTAssertEqual(self.notificationScheduler.scheduledNotifications.count, 1)
XCTAssertEqual(self.notificationScheduler.scheduledNotifications[0].title, "Good Morning!")
}
func testMorningNotification_after11_notificationIsNotScheduled() {
let timeProvider = TimeProviderStub(
date: DateComponents(hour: 12).date!,
calendarClosure: { id in return Calendar(identifier: id))
}
let notificationManager = NotificationManager(
notificationScheduler: self.notificationScheduler,
timeProvider: timeProvider
)
notificationManager.scheduleMorningNotification()
XCTAssertEqual(self.notificationScheduler.scheduledNotifications.count, 0)
}
// ... tests for the other methods
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment