Ich versuche Unit Tests für JSON-Validierung zu schreiben (da die App stark auf JSON von einer Rest API angewiesen ist).So lesen Sie in einer lokalen JSON-Datei zum Testen
Ich habe eine lokale Datei, die einfach json enthält: "goodFeaturedJson.txt"
Der Inhalt:
{
"test": "TEST"
}
Der Testfall:
- (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...");
}
Der Test jedes Mal versagt. Die Konsole druckt:
The json string is: (null)
Warum? Ich weiß, warum der Test fehlschlägt, da klar ist, wenn die Daten null/null sind, wird es kein gültiges json geben, und der Validator wird brechen (was es tun sollte, wenn es ungültig ist).
Es muss etwas einfaches hier fehlen, irgendwelche Ideen?
sollte ofType ist 'ofType: @" txt "'? –
Die Zeichenfolge ist immer noch null, wenn ich das tue. Ich habe versucht, Text und "Json" als Typ. Das Ergebnis scheint immer dasselbe zu sein und ich bin mir nicht sicher warum. –
Was sagt der Fehler (den Sie anscheinend nicht überprüfen)? –