Created
April 9, 2014 02:21
-
-
Save roothybrid7/10220115 to your computer and use it in GitHub Desktop.
iOSのユニットテスト(LogicTests)で和暦設定時のテストを行う ref: http://qiita.com/roothybrid7/items/7185073390e308d5862b
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "XXXTestCase.h" | |
#import <OCMock.h> | |
@interface XXXDateConvertTests : XXXTestCase | |
@property (nonatomic) NSString *dateString; | |
@end | |
@implementation XXXDateConvertTests | |
- (void)setUp | |
{ | |
[super setUp]; | |
self.dateString = @"20140408"; | |
} | |
- (void)tearDown | |
{ | |
// Put teardown code here. This method is called after the invocation of each test method in the class. | |
[super tearDown]; | |
} | |
// MARK: 日付変換処理だけ抜き出した | |
- (NSString *)postedAtWithDateFormatter:(NSDateFormatter *)dateFormatter | |
{ | |
NSInteger year = [self.dateString substringWithRange:NSMakeRange(0, 4)].integerValue; | |
NSInteger month = [self.dateString substringWithRange:NSMakeRange(4, 2)].integerValue; | |
[dateFormatter setDateFormat:@"yyyy-MM-dd"]; | |
// MARK: 和暦設定の場合、年を正常にパースできないのでカレンダーをグレゴリオ暦に変更 | |
// dateFormatter.calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; | |
NSDate *date = [dateFormatter dateFromString:[NSString stringWithFormat:@"%i-%i-1", year, month]]; | |
[dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"]]; | |
[dateFormatter setDateFormat:@"MMMM, yyyy"]; | |
return [dateFormatter stringFromDate:date]; | |
} | |
- (void)testPostedAt | |
{ | |
STAssertFalse([[[NSCalendar currentCalendar] calendarIdentifier] isEqualToString:NSJapaneseCalendar], @""); | |
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; | |
NSString *resultString = [self postedAtWithDateFormatter:[[NSDateFormatter alloc] init]]; | |
STAssertEqualObjects(resultString, @"April, 2014", @""); | |
[self createJPLocaleWithJapaneseCalendarMock]; // 和暦設定に変更 | |
NSLog(@"%@", dateFormatter.locale.localeIdentifier); | |
dateFormatter = [[NSDateFormatter alloc] init]; | |
resultString = [self postedAtWithDateFormatter:dateFormatter]; | |
STAssertFalse([resultString isEqualToString:@"April, 2014"], @""); // April, 4002になってしまう | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <SenTestingKit/SenTestingKit.h> | |
#import <OCMock/OCMock.h> | |
@interface XXXTestCase : SenTestCase | |
#pragma mark - NSLocale(ja_JP@calendar=japanese) | |
/*! | |
@method createJPLocaleWithJapaneseCalendarMock | |
@abstract iPhoneのカレンダー設定を和暦に書き換えたMockを作成 | |
@discussion | |
iPhoneの[設定]-[一般]-[言語環境]-[カレンダー] = 和暦 | |
*/ | |
- (id)createJPLocaleWithJapaneseCalendarMock; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface XXXTestCase () | |
@property (nonatomic) NSMutableArray *mocksToVerify; | |
@end | |
@implementation XXXTestCase | |
- (void)tearDown | |
{ | |
for (id mock in self.mocksToVerify) { | |
[mock verify]; | |
} | |
self.mocksToVerify = nil; | |
// [...] | |
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:XXXTestDomainName]; | |
[super tearDown]; | |
} | |
#pragma mark - NSLocale(ja_JP@calendar=japanese) | |
- (id)createJPLocaleWithJapaneseCalendarMock | |
{ | |
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:[NSString stringWithFormat:@"ja_JP@calendar=%@", | |
NSJapaneseCalendar]]; | |
id localeMock = [self autoVerifiedMockForClass:[NSLocale class]]; | |
[[[[localeMock stub] classMethod] andReturn:locale] systemLocale]; | |
[[[[localeMock stub] classMethod] andReturn:locale] currentLocale]; | |
[[[[localeMock stub] classMethod] andReturn:locale] autoupdatingCurrentLocale]; | |
return localeMock; | |
} | |
#pragma mark - Setup and verify mock: http://www.objc.io/issue-1/testing-view-controllers.html | |
- (void)verifyDuringTearDown:(id)mock | |
{ | |
if (!self.mocksToVerify) { | |
self.mocksToVerify = @[].mutableCopy; | |
} | |
[self.mocksToVerify addObject:mock]; | |
} | |
- (id)autoVerifiedMockForClass:(Class)aClass | |
{ | |
id mock = [OCMockObject mockForClass:aClass]; | |
[self verifyDuringTearDown:mock]; | |
return mock; | |
} | |
- (id)autoVerifiedPartialMockForObject:(id)object | |
{ | |
id mock = [OCMockObject partialMockForObject:object]; | |
[self verifyDuringTearDown:mock]; | |
return mock; | |
} | |
- (id)autoVerifiedNickMockForClass:(Class)aClass | |
{ | |
id mock = [OCMockObject niceMockForClass:aClass]; | |
[self verifyDuringTearDown:mock]; | |
return mock; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment