Ios 如何读入本地JSON文件进行测试

Ios 如何读入本地JSON文件进行测试,ios,objective-c,json,parsing,sentestingkit,Ios,Objective C,Json,Parsing,Sentestingkit,我正在尝试为json验证编写单元测试(因为应用程序严重依赖RESTAPI中的json) 我有一个包含简单json的本地文件:“goodFeaturedJson.txt” 内容包括: { "test": "TEST" } 测试用例: - (void)testJsonIsValid { Featured *featured = [Featured new]; NSString* filepath = [[NSBundle mainBundle]pathForResource:

我正在尝试为json验证编写单元测试(因为应用程序严重依赖RESTAPI中的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,并且验证器将中断(如果无效,则应该中断)


这里一定有我遗漏的简单内容,有什么想法吗?

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

guard let pathString = Bundle(for: type(of: self)).path(forResource: "UnitTestData", ofType: "json") else {
    fatalError("UnitTestData.json not found")
}
迅速地 Swift 5及以上版本

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)")
Swift 3和4

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的答案,该答案在目标C

中,如果类型为@“txt”,则字符串仍然为空。我尝试了文本,并将“json”作为类型。结果似乎总是一样的,我不知道为什么。错误(您显然不想检查)是怎么说的?也是*data null吗?JSONObjectWithData的数据参数是nil。(这是错误)。同样是的,NSData对象为零。该死,我没有意识到关于NSBundle;谢谢(这解决了所有问题)。感谢您指出这一点:`Bundle(for:type(of:self))`在其他任何地方都找不到,这使得它在单元测试中工作!