17

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

我在这里错过了一些简单的东西,有什么想法吗?

4

2 回答 2

32

在单元测试中你可能想要使用[NSBundle bundleForClass:[self class]],而不是[NSBundle mainBundle]. 那是因为单元测试不是一个独立的应用程序。你会得到类似的mainBundle东西/Applications/Xcode.app/Contents/Developer/Tools,但是 usingbundleForClass会得到你的单元测试类所在的包。

guard let pathString = Bundle(for: type(of: self)).path(forResource: "UnitTestData", ofType: "json") else {
    fatalError("UnitTestData.json not found")
}
于 2013-05-15T17:38:46.690 回答
22

在斯威夫特

斯威夫特 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)")

斯威夫特 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)")

斯威夫特 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 中的答案

于 2016-11-10T23:24:14.617 回答