Skip to content

Instantly share code, notes, and snippets.

@blakewatters
Created August 30, 2011 02:23
Show Gist options
  • Save blakewatters/1179996 to your computer and use it in GitHub Desktop.
Save blakewatters/1179996 to your computer and use it in GitHub Desktop.
RestKit Unit Testing Fixture Helpers
// Read a fixture from the app bundle
NSString* RKSpecReadFixture(NSString* fileName) {
NSError* error = nil;
NSString* filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
NSString* fixtureData = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
if (fixtureData == nil && error) {
[NSException raise:nil format:@"Failed to read contents of fixture '%@'. Did you add it to the app bundle? Error: %@", fileName, [error localizedDescription]];
}
return fixtureData;
}
NSString* RKSpecMIMETypeForFixture(NSString* fileName) {
NSString* extension = [[fileName pathExtension] lowercaseString];
if ([extension isEqualToString:@"xml"]) {
return RKMIMETypeXML;
} else if ([extension isEqualToString:@"json"]) {
return RKMIMETypeJSON;
} else {
return nil;
}
}
id RKSpecParseFixture(NSString* fileName) {
NSError* error = nil;
NSString* data = RKSpecReadFixture(fileName);
NSString* MIMEType = RKSpecMIMETypeForFixture(fileName);
id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:MIMEType];
id object = [parser objectFromString:data error:&error];
if (object == nil) {
RKLogCritical(@"Failed to parse JSON fixture '%@'. Error: %@", fileName, [error localizedDescription]);
return nil;
}
return object;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment