2013-05-15 10 views
11

Próbuję napisać testy jednostkowe dla sprawdzania poprawności json (ponieważ aplikacja silnie polega na jsonie z API odpoczynku).Jak czytać w lokalnym pliku JSON do testowania

Mam lokalnego pliku, który zawiera prosty json: "goodFeaturedJson.txt"

Zawartość:

{ 
    "test": "TEST" 
} 

sprawie Test:

- (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...");  
} 

test nie powiedzie się za każdym razem. Konsola drukuje:

The json string is: (null) 

Dlaczego? Wiem, dlaczego test się nie udał, ponieważ wyraźnie, jeśli dane są zerowe/zerowe, nie będzie prawidłowego json, a walidator ulegnie zerwaniu (co powinno, jeśli jest nieprawidłowe).

Musiało być coś prostego, czego tu brakowało, jakieś pomysły?

+0

powinien ofType be 'ofType: @" txt "'? –

+0

Ciąg znaków jest nadal pusty, jeśli to zrobię. Próbowałem tekstu i "json" jako typ. Wynik wydaje się być zawsze taki sam i nie jestem pewien dlaczego. –

+0

Co oznacza błąd (którego najwyraźniej nie próbujesz sprawdzić)? –

Odpowiedz

19

W teście jednostkowym prawdopodobnie chcesz użyć [NSBundle bundleForClass:[self class]], a nie [NSBundle mainBundle]. To dlatego, że test jednostkowy nie jest samodzielną aplikacją. Z wersją mainBundle otrzymujesz coś w rodzaju /Applications/Xcode.app/Contents/Developer/Tools, ale użycie numeru bundleForClass spowoduje pobranie pakietu, w którym znajduje się klasa testu urządzenia.

+0

Cholera, nie zdawałem sobie sprawy, że o NSBundle; dzięki! (To rozwiązało wszystko). –

4

w Swift

Swift 3 i powyżej

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)") 

* ten zawiera odpowiedzi Tom Harrington, który jest w Objective C