使用除 -[NSDate init] 之外的类别可以轻松地存根 NSDate 以返回模拟日期。-[NSDate init] 不像其他方法那样被调用。class_addMethod 没有帮助。method_exchangeImplementations, method_setImplementation on -[NSDate init] 实际上改变了 -[NSObject init] 但对 -[NSDate init] 没有影响。
[NSDate setMockDate:[NSDate dateWithTimeIntervalSinceReferenceDate:0]];
NSDate *date1 = [NSDate date];
NSLog(@"%@", date1);
NSLog(@"%.0f", [date1 timeIntervalSinceNow]);
// _replacement_Method is not called!
NSDate *date2 = [[NSDate alloc] init];
NSLog(@"%@", date2);
NSLog(@"%.0f", [date2 timeIntervalSinceNow]);
// _replacement_Method is called
NSObject *object = [[NSObject alloc] init];
NSLog(@"%@", object);
// A class with empty implementation to test inherited init from NSObject
// _replacement_Method is called by -[MyObject init]
MyObject *myobject = [[MyObject alloc] init];
NSLog(@"%@", myobject);
输出是
2001-01-01 00:00:00 +0000
-0
2014-11-26 14:43:26 +0000
438705806
<NSObject: 0x7fbc50e19d90>
<MyObject: 0x7fbc50e4ad30>
NSDate+Mock.m
#import "NSDate+Mock.h"
#import <mach/clock.h>
#import <mach/mach.h>
#import <objc/runtime.h>
static NSTimeInterval sTimeOffset;
static IMP __original_Method_Imp;
id _replacement_Method(id self, SEL _cmd)
{
return ((id(*)(id,SEL))__original_Method_Imp)(self, _cmd);
}
@implementation NSDate (Mock)
+ (NSObject *)lock
{
static dispatch_once_t onceToken;
static NSObject *lock;
dispatch_once(&onceToken, ^{
lock = [[NSObject alloc] init];
});
return lock;
}
+ (void)setMockDate:(NSDate *)date
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method m1 = class_getInstanceMethod([NSDate class], @selector(init));
Method m2 = class_getInstanceMethod([NSDate class], @selector(initMock));
// method_exchangeImplementations(m1, m2);
// class_addMethod([NSDate class], @selector(init), (IMP)_replacement_Method, "@@:");
__original_Method_Imp = method_setImplementation(m1, (IMP)_replacement_Method);
});
@synchronized([self lock]) {
sTimeOffset = [date timeIntervalSinceReferenceDate] - [self trueTimeIntervalSinceReferenceDate];
}
}
+ (NSTimeInterval)mockTimeOffset
{
@synchronized([self lock]) {
return sTimeOffset;
}
}
+ (NSTimeInterval)trueTimeIntervalSinceReferenceDate
{
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
NSTimeInterval now = mts.tv_sec + mts.tv_nsec * 1e-9 - NSTimeIntervalSince1970;
return now;
}
+ (NSTimeInterval)timeIntervalSinceReferenceDate
{
return [self trueTimeIntervalSinceReferenceDate] + [self mockTimeOffset];
}
+ (instancetype)date
{
return [[NSDate alloc] initWithTimeIntervalSinceNow:0];
}
+ (instancetype)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs
{
return [[NSDate alloc] initWithTimeIntervalSinceNow:secs];
}
//- (instancetype)init
//{
// self = [super init];
// return self;
//}
//- (instancetype)initMock
//{
// self = nil;
// NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:0];
// return date;
//}
- (instancetype)initWithTimeIntervalSinceNow:(NSTimeInterval)secs
{
return [self initWithTimeIntervalSinceReferenceDate:[NSDate timeIntervalSinceReferenceDate] + secs];
}
- (NSTimeInterval)timeIntervalSinceNow
{
NSTimeInterval t = [self timeIntervalSinceReferenceDate];
return t - [NSDate timeIntervalSinceReferenceDate];
}
@end