forked from Quick/Nimble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostNotificationTest.swift
87 lines (78 loc) · 3.39 KB
/
PostNotificationTest.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import XCTest
import Nimble
import Foundation
final class PostNotificationTest: XCTestCase {
let notificationCenter = NotificationCenter()
func testPassesWhenNoNotificationsArePosted() {
expect {
// no notifications here!
return nil
}.to(postNotifications(beEmpty()))
}
func testPassesWhenExpectedNotificationIsPosted() {
let testNotification = Notification(name: Notification.Name("Foo"), object: nil)
expect {
self.notificationCenter.post(testNotification)
}.to(postNotifications(equal([testNotification]), from: notificationCenter))
}
func testPassesWhenAllExpectedNotificationsArePosted() {
let foo = 1 as NSNumber
let bar = 2 as NSNumber
let n1 = Notification(name: Notification.Name("Foo"), object: foo)
let n2 = Notification(name: Notification.Name("Bar"), object: bar)
expect {
self.notificationCenter.post(n1)
self.notificationCenter.post(n2)
return nil
}.to(postNotifications(equal([n1, n2]), from: notificationCenter))
}
func testFailsWhenNoNotificationsArePosted() {
let testNotification = Notification(name: Notification.Name("Foo"), object: nil)
failsWithErrorMessage("expected to equal <[\(testNotification)]>, got no notifications") {
expect {
// no notifications here!
return nil
}.to(postNotifications(equal([testNotification]), from: self.notificationCenter))
}
}
func testFailsWhenNotificationWithWrongNameIsPosted() {
let n1 = Notification(name: Notification.Name("Foo"), object: nil)
let n2 = Notification(name: Notification.Name(n1.name.rawValue + "a"), object: nil)
failsWithErrorMessage("expected to equal <[\(n1)]>, got <[\(n2)]>") {
expect {
self.notificationCenter.post(n2)
return nil
}.to(postNotifications(equal([n1]), from: self.notificationCenter))
}
}
func testFailsWhenNotificationWithWrongObjectIsPosted() {
let n1 = Notification(name: Notification.Name("Foo"), object: nil)
let n2 = Notification(name: n1.name, object: NSObject())
failsWithErrorMessage("expected to equal <[\(n1)]>, got <[\(n2)]>") {
expect {
self.notificationCenter.post(n2)
return nil
}.to(postNotifications(equal([n1]), from: self.notificationCenter))
}
}
func testPassesWhenExpectedNotificationEventuallyIsPosted() {
let testNotification = Notification(name: Notification.Name("Foo"), object: nil)
expect {
deferToMainQueue {
self.notificationCenter.post(testNotification)
}
return nil
}.toEventually(postNotifications(equal([testNotification]), from: notificationCenter))
}
#if os(macOS)
func testPassesWhenAllExpectedNotificationsarePostedInDistributedNotificationCenter() {
let center = DistributedNotificationCenter()
let n1 = Notification(name: Notification.Name("Foo"), object: "1")
let n2 = Notification(name: Notification.Name("Bar"), object: "2")
expect { () -> Void in
center.post(n1)
center.post(n2)
}.toEventually(postDistributedNotifications(equal([n1, n2]), from: center, names: [n1.name, n2.name]))
}
#endif
}