Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/divinedominion/testingtoolkit
Wrappers for common Cocoa objects to replace instances during tests.
https://github.com/divinedominion/testingtoolkit
Last synced: 17 days ago
JSON representation
Wrappers for common Cocoa objects to replace instances during tests.
- Host: GitHub
- URL: https://github.com/divinedominion/testingtoolkit
- Owner: DivineDominion
- License: mit
- Created: 2014-10-01T15:32:04.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2020-10-10T15:52:58.000Z (about 4 years ago)
- Last Synced: 2023-04-14T07:22:51.944Z (over 1 year ago)
- Language: Objective-C
- Homepage:
- Size: 28.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TestingToolkit for Mac and iOS
A collection of common Cocoa singleton wrappers which are useful for unit-testing if your objects work well with:
- `NSUserDefaults`
- `NSNotificationCenter`
- `NSCalendar`'s `-autoupdatingCalendar` and `-autoupdatingLocale`## Usage
### `NSNotificationCenter` Test Double/Recorder
To record and play back sent notifications from a test case, add a custom `NSNotificationCenter` subclass. (I prefer to inherit from `NSObject` to avoid unwanted side-effects and add methods until the informal protocol is satisfied. The following should give a good head start.)
```obj-c
@interface TestNotificationCenter : NSObject
@property (nonatomic, strong) NSMutableArray *notifications;
@property (nonatomic, readonly) BOOL didReceiveNotifications;
@end@implementation TestNotificationCenter
- (NSArray *)notifications {
if (!_notifications) {
_notifications = [NSMutableArray array];
}return _notifications;
}- (BOOL)didReceiveNotifications {
return self.notifications && self.notifications.count > 0;
}- (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo {
NSDictionary *notification = @{@"name" : aName, @"object" : anObject, @"userInfo" : aUserInfo};
[self.notifications addObject:notification];
}- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject { /* no op */ }
- (void)removeObserver:(id)observer { /* no op */ }
- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject { /* no op */ }
@end
```Then use the test double like this:
```obj-c
@interface ExampleTests : XCTestCase
@end@implementation ExampleTests
{
id objectUnderTest;
TestNotificationCenter *testNotificationCenter;
}- (void)setUp {
[super setUp];testNotificationCenter = [[TestNotificationCenter alloc] init];
[CTKNotificationCenter setSharedInstance:[CTKNotificationCenter notificationCenterWith:testNotificationCenter]];
objectUnderTest = [[ExampleObject alloc] init];
}- (void)tearDown {
[CTKNotificationCenter resetSharedInstance];
[super tearDown];
}- (void)testDoingSomething_DoesntBroadcast {
[objectUnderTest performSomeAction];XCTAssertFalse(testNotificationCenter.didReceiveNotifications);
}
// ...
@end
```Ensure that you obtain `NSNotificationCenter` throughout your application by calling `[CTKNotificationCenter defaultCenter]`.
# License
MIT license. For details, see `LICENSE` file.