2013-05-15 9 views
11

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?

+0

sollte ofType ist 'ofType: @" txt "'? –

+0

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. –

+0

Was sagt der Fehler (den Sie anscheinend nicht überprüfen)? –

Antwort

19

In einem Komponententest möchten Sie wahrscheinlich [NSBundle bundleForClass:[self class]] und nicht [NSBundle mainBundle] verwenden. Der Unit Test ist keine eigenständige App. Mit mainBundle erhalten Sie etwas wie , aber mit bekommen Sie das Bündel, wo Ihre Einheit Testklasse befindet.

+0

Verdammt, wusste das nicht über NSBundle; Vielen Dank! (Das hat alles gelöst). –

4

In Swift

Swift 3 und höher

guard let pathString = Bundle(for: type(of: self)).path(forResource: "UnitTestData", ofType: "json") else { 
    fatalError("UnitTestData.json not found") 
} 

guard let jsonString = try? NSString(contentsOfFile: pathString, encoding: String.Encoding.utf8.rawValue) else { 
    fatalError("Unable to convert UnitTestData.json to String") 
} 

print("The JSON string is: \(jsonString)") 

guard let jsonData = jsonString.data(using: String.Encoding.utf8.rawValue) else { 
    fatalError("Unable to convert UnitTestData.json to NSData") 
} 

guard let jsonDictionary = try? JSONSerialization.jsonObject(with: jsonData, options: []) as? [String:AnyObject] else { 
    fatalError("Unable to convert UnitTestData.json to JSON dictionary") 
} 

print("The JSON dictionary is: \(jsonDictionary)") 

Swift 2,2

guard let pathString = NSBundle(forClass: self.dynamicType).pathForResource("UnitTestData", ofType: "json") else { 
     fatalError("UnitTestData.json not found") 
    } 

    guard let jsonString = try? NSString(contentsOfFile: pathString, encoding: NSUTF8StringEncoding) else { 
     fatalError("Unable to convert UnitTestData.json to String") 
    } 

    print("The JSON string is: \(jsonString)") 

    guard let jsonData = jsonString.dataUsingEncoding(NSUTF8StringEncoding) else { 
     fatalError("Unable to convert UnitTestData.json to NSData") 
    } 

    guard let jsonDictionary = try? NSJSONSerialization.JSONObjectWithData(jsonData, options: []) as? [String:AnyObject] else { 
     fatalError("Unable to convert UnitTestData.json to JSON dictionary") 
    } 

    print("The JSON dictionary is: \(jsonDictionary)") 

* Dieser enthält die Antwort von Tom Harrington, die in Objective C ist