存根 - [NSDate init]

时间:2014-11-26 14:58:23

标签: objective-c mocking stub objective-c-category method-swizzling

使用除[ - 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

2 个答案:

答案 0 :(得分:4)

NSDate具有以下重要特征:

  • 这是一个类集群
  • 这是不可改变的

在这种情况下,+alloc仅返回占位符,并将-init…发送到该占位符(类__NSPlaceholderDate)。如果-initNSDate或未执行任何内容,则替换-init__NSPlaceholderDate)无效。

这是因为+alloc无法决定选择哪个(私有)子类,因为它没有参数。 (它们在-init…传递。)

你可以

  • 只需替换-init
  • __NSPlaceholderDate即可
  • 替换-init返回的+alloc
  • 替换+alloc以返回您的私人占位符并覆盖其中的-init

答案 1 :(得分:2)

如果您需要模拟日期,例如在测试中,考虑使用Factory pattern实例化NSDate对象并替换工厂进行生产或测试。这样,只有你自己的类最终会得到模拟日期,你不必担心意外地替换Apple框架可能使用的方法。