How to use testPassesWhenExpectedNotificationIsPosted method of PostNotificationTest class

Best Nimble code snippet using PostNotificationTest.testPassesWhenExpectedNotificationIsPosted

Run Nimble automation tests on LambdaTest cloud grid

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

PostNotificationTest.swift

Source: PostNotificationTest.swift Github

copy
1import XCTest
2import Nimble
3import Foundation
4
5final class PostNotificationTest: XCTestCase, XCTestCaseProvider {
6    static var allTests: [(String, (PostNotificationTest) -> () throws -> Void)] {
7        return [
8            ("testPassesWhenNoNotificationsArePosted", testPassesWhenNoNotificationsArePosted),
9            ("testPassesWhenExpectedNotificationIsPosted", testPassesWhenExpectedNotificationIsPosted),
10            ("testPassesWhenAllExpectedNotificationsArePosted", testPassesWhenAllExpectedNotificationsArePosted),
11            ("testFailsWhenNoNotificationsArePosted", testFailsWhenNoNotificationsArePosted),
12            ("testFailsWhenNotificationWithWrongNameIsPosted", testFailsWhenNotificationWithWrongNameIsPosted),
13            ("testFailsWhenNotificationWithWrongObjectIsPosted", testFailsWhenNotificationWithWrongObjectIsPosted),
14            ("testPassesWhenExpectedNotificationEventuallyIsPosted", testPassesWhenExpectedNotificationEventuallyIsPosted),
15        ]
16    }
17
18    let notificationCenter = NotificationCenter()
19
20    func testPassesWhenNoNotificationsArePosted() {
21        expect {
22            // no notifications here!
23            return nil
24        }.to(postNotifications(beEmpty(), fromNotificationCenter: notificationCenter))
25    }
26
27    func testPassesWhenExpectedNotificationIsPosted() {
28        let testNotification = Notification(name: Notification.Name("Foo"), object: nil)
29        expect {
30            self.notificationCenter.post(testNotification)
31        }.to(postNotifications(equal([testNotification]), fromNotificationCenter: notificationCenter))
32    }
33
34    func testPassesWhenAllExpectedNotificationsArePosted() {
35        let foo = NSNumber(value: 1)
36        let bar = NSNumber(value: 2)
37        let n1 = Notification(name: Notification.Name("Foo"), object: foo)
38        let n2 = Notification(name: Notification.Name("Bar"), object: bar)
39        expect {
40            self.notificationCenter.post(n1)
41            self.notificationCenter.post(n2)
42            return nil
43        }.to(postNotifications(equal([n1, n2]), fromNotificationCenter: notificationCenter))
44    }
45
46    func testFailsWhenNoNotificationsArePosted() {
47        let testNotification = Notification(name: Notification.Name("Foo"), object: nil)
48        failsWithErrorMessage("expected to equal <[\(testNotification)]>, got no notifications") {
49            expect {
50                // no notifications here!
51                return nil
52            }.to(postNotifications(equal([testNotification]), fromNotificationCenter: self.notificationCenter))
53        }
54    }
55
56    func testFailsWhenNotificationWithWrongNameIsPosted() {
57        let n1 = Notification(name: Notification.Name("Foo"), object: nil)
58        let n2 = Notification(name: Notification.Name(n1.name.rawValue + "a"), object: nil)
59        failsWithErrorMessage("expected to equal <[\(n1)]>, got <[\(n2)]>") {
60            expect {
61                self.notificationCenter.post(n2)
62                return nil
63            }.to(postNotifications(equal([n1]), fromNotificationCenter: self.notificationCenter))
64        }
65    }
66
67    func testFailsWhenNotificationWithWrongObjectIsPosted() {
68        let n1 = Notification(name: Notification.Name("Foo"), object: nil)
69        let n2 = Notification(name: n1.name, object: NSObject())
70        failsWithErrorMessage("expected to equal <[\(n1)]>, got <[\(n2)]>") {
71            expect {
72                self.notificationCenter.post(n2)
73                return nil
74            }.to(postNotifications(equal([n1]), fromNotificationCenter: self.notificationCenter))
75        }
76    }
77
78    func testPassesWhenExpectedNotificationEventuallyIsPosted() {
79        #if _runtime(_ObjC)
80            let testNotification = Notification(name: Notification.Name("Foo"), object: nil)
81            expect {
82                deferToMainQueue {
83                    self.notificationCenter.post(testNotification)
84                }
85                return nil
86            }.toEventually(postNotifications(equal([testNotification]), fromNotificationCenter: notificationCenter))
87        #else
88            print("\(#function) is missing because toEventually is not implement on this platform")
89        #endif
90    }
91}
92
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

Trigger testPassesWhenExpectedNotificationIsPosted code on LambdaTest Cloud Grid

Execute automation tests with testPassesWhenExpectedNotificationIsPosted 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)