Car*_*loS 10 cocoa cocoa-touch unit-testing nsdate
我正在测试一个调用的方法-[NSDate date]
,并使用系统时间作为参考来做一些逻辑.我需要测试一年中某些特定日子的逻辑是否正确,但我不能等到那一天.那么我可以在单元测试时以编程方式更改系统日期吗?
Rob*_*vić 13
您需要使用Mock对象,在Objective C中,您可以通过仅覆盖所需的方法轻松地对类别进行操作.例如,您可以使用以下类别:
// ------------- File: NSDate+NSDateMock.h
@interface NSDate (NSDateMock)
+(void)setMockDate:(NSString *)mockDate;
+(NSDate *) mockCurrentDate;
@end
// -------------- File: NSDate+NSDateMock.m
#import "NSDate+NSDateMock.h"
@implementation NSDate (NSDateMock)
static NSDate *_mockDate;
+(NSDate *)mockCurrentDate
{
return _mockDate;
}
+(void)setMockDate:(NSString *)mockDate
{
_mockDate = [NSDate dateWithString:mockDate];
}
@end
Run Code Online (Sandbox Code Playgroud)
此外,您还需要SwizzleClassMethod
void SwizzleClassMethod(Class c, SEL orig, SEL new) {
Method origMethod = class_getClassMethod(c, orig);
Method newMethod = class_getClassMethod(c, new);
c = object_getClass((id)c);
if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
else
method_exchangeImplementations(origMethod, newMethod);
}
Run Code Online (Sandbox Code Playgroud)
然后在您的单元测试中,您将能够像这样使用它
SwizzleClassMethod([NSDate class], @selector(date), @selector(mockCurrentDate));
[NSDate setMockDate:@"2007-03-24 10:45:32 +0200"];
NSLog(@"Date is: %@", [NSDate date]);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1409 次 |
最近记录: |