PBDNotificationObserver 0.0.5

PBDNotificationObserver 0.0.5

测试验证过
语种语言 Obj-CObjective C
许可 MIT
发布最后发布2016年3月

Pawel Dudek 维护。



这是一个小型辅助类,它帮助编写测试,以确保是否已发布特定通知。

动机

假设我们想要测试我们的对象是否发布了特定通知。您通常会通过注册块观察者并检查它是否捕获了实际通知来完成此操作

NSNotification *capturedNotification = nil;

id <NSObject> token = [[NSNotificationCenter defaultCenter] addObserverForName:SomeNotification
                                                                        object:anObject
                                                                         queue:nil
                                                                    usingBlock:^(NSNotification *note) {
                                                                        capturedNotification = note;
                                                                    }];

// Run code that posts the notification

XCTAssertNotNil(capturedNotification);

[[NSNotificationCenter defaultCenter] removeObserver:token];

这需要很多代码,这实际上减少了代码的可读性,因此您(和其他开发人员)需要更多的注意力来理解这里发生的事情。

这个小工具通过引入通知观察者来解决此问题

PBDNotificationObserver *observer = [PBDNotificationObserver new];

// Run code that posts the notification

verifyNotification(observer, SomeNotification);

您还可以传递 OCHamcrest 匹配器而不是通知名称。

用法

您可以将 PBDNotificationObserver 直接添加到您的项目中,或者使用 Cocoapods

pod 'PBDNotificationObserver', '0.0.1'