快速/灵活的通知用户信息测试 [英] Quick/Nimble notification userInfo testing

查看:21
本文介绍了快速/灵活的通知用户信息测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看快速/灵活的通知支持,例如:

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

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

有没有办法让我处理返回的通知以检查 userInfo?我的目标是调用一个发布通知的方法,然后检查该通知 userInfo 并确保键/值对正确.

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.

快速:2.1.0
敏捷:8.0.1

Quick: 2.1.0
Nimble: 8.0.1

推荐答案

我最终这样做了:

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..

这篇关于快速/灵活的通知用户信息测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆