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

Jor*_*hns 15 parsing json objective-c ios sentestingkit

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

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

内容:

{
  "test": "TEST"
}
Run Code Online (Sandbox Code Playgroud)

测试用例:

- (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...");    
}
Run Code Online (Sandbox Code Playgroud)

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

The json string is: (null)
Run Code Online (Sandbox Code Playgroud)

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

我必须在这里找到一些简单的东西,任何想法?

Tom*_*ton 26

在单元测试中,您可能想要使用[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")
}
Run Code Online (Sandbox Code Playgroud)


Mob*_*Dan 11

在斯威夫特

斯威夫特3及以上

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

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

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

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

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

print("The JSON dictionary is: \(jsonDictionary)")
Run Code Online (Sandbox Code Playgroud)

Swift 2.2

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)")
Run Code Online (Sandbox Code Playgroud)

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