3

I was looking at the notifications support in quick/nimble, e.g.:

expect {
    NotificationCenter.default.postNotification(testNotification)
}.to(postNotifications(equal([testNotification]))

Is there a way to get my hands on the notification that is returned to inspect the userInfo? My goal is to invoke a method that posts a notification, and then inspect that notification userInfo and make sure that the key/value pairs are correct.

Quick: 2.1.0
Nimble: 8.0.1

Zsolt
  • 3,648
  • 3
  • 32
  • 47

1 Answers1

1

I ended up doing this:

func equalName(_ expectedName: Notification.Name, condition: @escaping (([AnyHashable: Any]) -> Bool)) -> Predicate<[Notification]> {
    return Predicate.define("equal <\(stringify(expectedName))>") { actualExpression, msg in
        guard let actualValue = try actualExpression.evaluate() else {
            return PredicateResult(status: .fail, message: msg)
        }

        let actualNames = actualValue
            .filter { $0.name == expectedName }
            .filter { notification in
                guard let userInfo = notification.userInfo else {
                    return false
                }

                return condition(userInfo)
            }
            .compactMap { $0.name }
        let matches = actualNames.contains(expectedName)
        return PredicateResult(bool: matches, message: msg)
    }
}

and then at the call site you can provide the condition..

Zsolt
  • 3,648
  • 3
  • 32
  • 47