Created
April 8, 2014 16:42
-
-
Save ksmandersen/10153750 to your computer and use it in GitHub Desktop.
Load JSON fixture files for your XCTests
This file contains 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 JSONFixture : NSObject | |
+ (id)fixtureDataWithName:(NSString *)fixtureName; | |
@end | |
@implementation JSONFixture | |
+ (id)fixtureDataWithName:(NSString *)fixtureName { | |
NSError *error; | |
NSString *filePath = [self fixturePathForName:fixtureName]; | |
if (!filePath) { | |
return nil; | |
} | |
NSData *fixtureData = [NSData dataWithContentsOfFile:filePath | |
options:0 | |
error:&error]; | |
if (error) { | |
return nil; | |
} | |
id jsonData = [NSJSONSerialization JSONObjectWithData:fixtureData options:0 error:&error]; | |
if (error) { | |
return nil; | |
} | |
return jsonData; | |
} | |
+ (NSString *)fixturePathForName:(NSString *)name { | |
return [[self testBundle] pathForResource:name ofType:@"json"]; | |
} | |
+ (NSBundle *)testBundle { | |
return [NSBundle bundleForClass:[self class]]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment