如何读取测试本地JSON文件(How to read in a local JSON file fo

2019-09-03 11:03发布

我试图写一个JSON验证单元测试(因为应用程序在很大程度上依赖于JSON从静止API)。

我有一个包含简单的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)

为什么? 我知道为什么测试失败,因为很明显,如果数据是零/零不会有有效的JSON,并验证将打破(这是它应该如果无效)。

必须有简单的东西我错过了这里,任何想法?

Answer 1:

在单元测试中,你可能想使用[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")
}


Answer 2:

在斯威夫特

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

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

*本采用了汤姆·哈灵顿的答案是在目标C



文章来源: How to read in a local JSON file for testing