I'm trying to write unit tests for json validation (since the app heavily relies on json from a rest API).
I have a local file that contains simple json: "goodFeaturedJson.txt"
The contents:
{
"test": "TEST"
}
The test case:
- (void)testJsonIsValid
{
Featured *featured = [Featured new];
NSString* filepath = [[NSBundle mainBundle]pathForResource:@"goodFeaturedJson" ofType:@"text"];
NSData *data = [NSData dataWithContentsOfFile:filepath];
NSString *jsonString = [[NSString alloc] initWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:nil];//[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"The json string is: %@", jsonString);
id JSON = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
STAssertTrue([featured jsonIsValid:JSON], @"Featured Data is NOT valid...");
}
The test fails every time. The console prints:
The json string is: (null)
Why? I know why the test is failing, since clearly if the data is nil/null there will be no valid json, and the validator will break (which it should if its invalid).
There must be something simple I missed here, any ideas?
ofType:@"txt"
? – Ganesha