How to use startObserving method of NotificationCollector class

Best Nimble code snippet using NotificationCollector.startObserving

Run Nimble automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

PostNotification.swift

Source: PostNotification.swift Github

copy
1import Foundation
2
3internal class NotificationCollector {
4    private(set) var observedNotifications: [Notification]
5    private let notificationCenter: NotificationCenter
6    private var token: NSObjectProtocol?
7
8    required init(notificationCenter: NotificationCenter) {
9        self.notificationCenter = notificationCenter
10        self.observedNotifications = []
11    }
12
13    func startObserving() {
14        // swiftlint:disable:next line_length
15        self.token = self.notificationCenter.addObserver(forName: nil, object: nil, queue: nil) { [weak self] notification in
16            // linux-swift gets confused by .append(n)
17            self?.observedNotifications.append(notification)
18        }
19    }
20
21    deinit {
22        if let token = self.token {
23            self.notificationCenter.removeObserver(token)
24        }
25    }
26}
27
28private let mainThread = pthread_self()
29
30public func postNotifications(
31    _ predicate: Predicate<[Notification]>,
32    fromNotificationCenter center: NotificationCenter = .default
33) -> Predicate<Any> {
34    _ = mainThread // Force lazy-loading of this value
35    let collector = NotificationCollector(notificationCenter: center)
36    collector.startObserving()
37    var once: Bool = false
38
39    return Predicate { actualExpression in
40        let collectorNotificationsExpression = Expression(
41            memoizedExpression: { _ in
42                return collector.observedNotifications
43            },
44            location: actualExpression.location,
45            withoutCaching: true
46        )
47
48        assert(pthread_equal(mainThread, pthread_self()) != 0, "Only expecting closure to be evaluated on main thread.")
49        if !once {
50            once = true
51            _ = try actualExpression.evaluate()
52        }
53
54        let actualValue: String
55        if collector.observedNotifications.isEmpty {
56            actualValue = "no notifications"
57        } else {
58            actualValue = "<\(stringify(collector.observedNotifications))>"
59        }
60
61        var result = try predicate.satisfies(collectorNotificationsExpression)
62        result.message = result.message.replacedExpectation { message in
63            return .expectedCustomValueTo(message.expectedMessage, actualValue)
64        }
65        return result
66    }
67}
68
69public func postNotifications<T>(
70    _ notificationsMatcher: T,
71    fromNotificationCenter center: NotificationCenter = .default)
72    -> Predicate<Any>
73    where T: Matcher, T.ValueType == [Notification]
74{
75    _ = mainThread // Force lazy-loading of this value
76    let collector = NotificationCollector(notificationCenter: center)
77    collector.startObserving()
78    var once: Bool = false
79
80    return Predicate { actualExpression in
81        let collectorNotificationsExpression = Expression(memoizedExpression: { _ in
82            return collector.observedNotifications
83            }, location: actualExpression.location, withoutCaching: true)
84
85        assert(pthread_equal(mainThread, pthread_self()) != 0, "Only expecting closure to be evaluated on main thread.")
86        if !once {
87            once = true
88            _ = try actualExpression.evaluate()
89        }
90
91        let failureMessage = FailureMessage()
92        let match = try notificationsMatcher.matches(collectorNotificationsExpression, failureMessage: failureMessage)
93        if collector.observedNotifications.isEmpty {
94            failureMessage.actualValue = "no notifications"
95        } else {
96            failureMessage.actualValue = "<\(stringify(collector.observedNotifications))>"
97        }
98        return PredicateResult(bool: match, message: failureMessage.toExpectationMessage())
99    }
100}
101
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in NotificationCollector

Trigger startObserving code on LambdaTest Cloud Grid

Execute automation tests with startObserving on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)