Skip to content

Instantly share code, notes, and snippets.

@ksmandersen
Created April 8, 2014 16:42
Show Gist options
  • Save ksmandersen/10153750 to your computer and use it in GitHub Desktop.
Save ksmandersen/10153750 to your computer and use it in GitHub Desktop.
Load JSON fixture files for your XCTests
@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