ios - 快速/灵活的通知用户信息测试
问题描述
我正在快速/灵活地查看通知支持,例如:
expect {
NotificationCenter.default.postNotification(testNotification)
}.to(postNotifications(equal([testNotification]))
有没有办法让我掌握返回以检查 userInfo 的通知?我的目标是调用一个发布通知的方法,然后检查该通知 userInfo 并确保键/值对正确。
快速:2.1.0
敏捷: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)
}
}
然后在呼叫站点您可以提供条件..
推荐阅读
- reactjs - 大量组件正在更新,但状态没有任何相关变化
- ffmpeg - ffmpeg avcodec_send_packet/avcodec_receive_frame 内存泄漏
- animation - 如何在 SkiaSharp 中获得流畅的动画
- html - 将输入文本放入图像中并使其具有响应性(css)
- ios - 将 Firebase(GoogleService-Info.plist 和 API 密钥)迁移到 iOS App Store 上的生产环境?
- visual-studio - Visual Studio 2017 Xamarin Designer 不显示内容,即使它以前有
- java - 在改造2中使用带有对象数组的json
- javascript - 为什么我的 React 站点在注册或登录后不会重定向到主页?
- javascript - 如何一个接一个地访问网址列表?
- c# - 安卓进度条消失