-
-
Save jeksys/226f73bb805c7f9c8b2d0241d2e18fd9 to your computer and use it in GitHub Desktop.
XCTest - Assert notification (not) triggered.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
class NotificationTestCase: XCTestCase { | |
func testTriggerNotification() { | |
expectation(forNotification: .fooBar, | |
object: nil, | |
handler: nil) | |
let notificationCenter = NotificationCenter.default | |
notificationCenter.post(name: .fooBar, | |
object: self) | |
waitForExpectations(timeout: 0.1, handler: nil) | |
} | |
func testDidNotTriggerNotification() { | |
let expectation = self.expectation(forNotification: .fooBar, | |
object: nil, | |
handler: nil) | |
expectation.isInverted = true | |
// Run code that should not trigger a notification | |
if false { | |
let notificationCenter = NotificationCenter.default | |
notificationCenter.post(name: .fooBar, | |
object: nil) | |
} | |
waitForExpectations(timeout: 0.1, handler: nil) | |
} | |
} | |
extension Notification.Name { | |
static let fooBar = Notification.Name(rawValue: "fooBar") | |
} | |
// Run tests in playground | |
NotificationTestCase.defaultTestSuite.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment