如何读取本地JSON文件以进行测试

时间:2013-05-15 17:06:37

标签: ios objective-c json parsing sentestingkit

我正在尝试为json验证编写单元测试(因为app严重依赖于来自其余API的json)。

我有一个包含简单json的本地文件:“goodFeaturedJson.txt”

内容:

{
  "test": "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...");    
}

每次测试失败。控制台打印:

The json string is: (null)

为什么呢?我知道为什么测试失败了,因为很明显,如果数据是nil / null,那么就没有有效的json,并且验证器将会中断(如果它无效则应该中断)。

这里有一些我想念的简单的想法吗?

2 个答案:

答案 0 :(得分:25)

在单元测试中,您可能希望使用[NSBundle bundleForClass:[self class]],而不是[NSBundle mainBundle]。那是因为单元测试不是一个独立的应用程序。使用mainBundle可以获得类似/Applications/Xcode.app/Contents/Developer/Tools的内容,但使用bundleForClass可以获得单元测试类所在的包。

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

答案 1 :(得分:9)

在Swift中

Swift 3及以上

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

*这包含Tom Harrington在Objective C中的回答