Skip to content

Instantly share code, notes, and snippets.

@Abizern
Created July 30, 2012 11:56
Show Gist options
  • Save Abizern/3206422 to your computer and use it in GitHub Desktop.
Save Abizern/3206422 to your computer and use it in GitHub Desktop.
Fake Notification Centre

FakeNotificationCentre

A ghetto mock for ease of unit testing methods that interact with the NotificationCentre

Based on the work by Graham Lee in Test-Driven iOS Development

//
// FakeNotificationCentre.h
//
// After Graham Lee
//
// Ghetto mock for unit testing.
#import <Foundation/Foundation.h>
@interface FakeNotificationCentre : NSObject
#pragma mark - Observers
- (void)addObserver: (id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
- (void)removeObserver: (id)observer;
- (void)removeObserver: (id)observer name: (NSString *)aName object: (id)obj;
- (BOOL)hasObject: (id)observer forNotification: (NSString *)aName;
#pragma mark - Notifications
- (void)postNotification:(NSNotification *)notification;
- (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfo;
- (BOOL)didReceiveNotification: (NSString *)name fromObject: (id)obj;
//- (BOOL)didReceiveNotification:(NSString *)name object:(id)obj userInfo:(NSDictionary *)userInfo;
@end
//
// FakeNotificationCentre.m
//
// After Graham Lee
//
// Ghetto mock for unit testing.
#import "FakeNotificationCentre.h"
#import <SenTestingKit/SenTestCase_Macros.h>
@implementation FakeNotificationCentre {
NSMutableDictionary *_observers;
NSMutableArray *_notifications;
}
#pragma mark - Set up and tear down
- (id)init {
if (!(self = [super init])) {
return nil; // Bail!
}
_observers = [NSMutableDictionary new];
_notifications = [NSMutableArray new];
return self;
}
#pragma mark - Observers
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject {
[_observers setObject:observer forKey:aName];
}
- (void)removeObserver:(id)observer {
[[_observers copy] enumerateKeysAndObjectsUsingBlock: ^(id key, id obj, BOOL *stop) {
if ([obj isEqual:observer]) {
[_observers removeObjectForKey: key];
}
}];
}
- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)obj {
[self removeObserver:observer];
}
- (BOOL)hasObject:(id)observer forNotification:(NSString *)aName {
return [[_observers objectForKey:aName] isEqual:observer];
}
#pragma mark - Notifications
- (void)postNotification:(NSNotification *)notification {
[_notifications addObject:notification];
}
- (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfo {
NSNotification *note = [NSNotification notificationWithName:notificationName object:notificationSender userInfo:userInfo];
[self postNotification:note];
}
- (BOOL)didReceiveNotification:(NSString *)name fromObject:(id)obj {
for (NSNotification *note in _notifications) {
if ([[note name] isEqualToString:name] && [[note object] isEqual:obj]) {
return YES;
}
}
return NO;
}
@end
@Abizern
Copy link
Author

Abizern commented Jul 30, 2012

Still more methods to implement, and figure out how to test the removal of objects from the notification centre.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment