NSDate+SRGFekable is a category to fake results of NSDate. You can test your app features that depend on date more easily.
Add the following line to your podfile and run pod update
.
pod 'NSDate+SRGFekable'
At first you need to include header file.
#import 'NSDate+SRGFekable'
You can fake NSDate simply
// You can fake result of NSDate.
[NSDate fakeWithString:@"2014/12/27 10:00:00"];
// On this process, NSDate behave as if current-time is "2014/12/27 10:00:00"
NSLog(@"now:%@",[NSDate date]); // -> now:2014/12/27 10:00:00
By using this category , you can write unit tests easily.
....
- (void)testIsXmas {
SeasonalEvent *seasonalEvent = [SeasonalEvent new];
[NSDate fakeWithString:@"2014/12/20 10:00:00"];
XCTAssertFalse( seasonalEvent.isXmas );
[NSDate fakeWithString:@"2014/12/25 10:00:00"];
XCTAssertTrue( seasonalEvent.isXmas );
}
...
You can fake from another NSDate instance..
NSDate *aDate = [NSDate dateWithTimeIntervalSinceNow:100];
[NSDate fakeWithDate:aDate];
// NSDate behave as if current-time is aDate
// NSDate behave as if current-time is 60 seconds latter of real date.
[NSDate fakeWithDelta:100];
// NSDate behave as if current-time is 180(=120+60) seconds latter of real date.
[NSDate fakeWithDelta:120];
//// NSDate behave as if current-time is 150(=180-30) seconds latter of real date.
[NSDate fakeWithDelta:-30];
// You can stop faking.
[NSDate stopFaking];
// You can get status of faking or not.
BOOL doFaking = [NSDate doFaking];
Each fakeXXX
methods have freeze
option. (Default is YES
)
- If
freeze
isYES
、faked date is completely stop. - If
freeze
isNO
, faked date go on.
This option may be useful when you fake date on Main Project(not Test Project) for manually test.
// YES is default.
[NSDate fakeWithString:@"2014/12/27 10:00:00" freeze:YES];
// -> [NSDate date] always return @"2014/12/27 10:00:00"
// freeze:NO
[NSDate fakeWithString:@"2014/12/27 10:00:00" freeze:NO];
// -> If elapsed 10 seconds after call above, [NSDate date] returns @"2014/12/27 10:00:10"
NSDate+SRGFekable is released under the MIT license. See LICENSE for details.