Как читать в локальном файле JSON для тестирования
Я пытаюсь написать модульные тесты для проверки 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)
Почему? Я знаю, почему тест терпит неудачу, так как четко, если данные равны nil/null, не будет действительного json, и валидатор сломается (что должно быть, если оно недействительно).
Должно быть что-то простое, что я пропустил здесь, любые идеи?
Ответы
Ответ 1
В модульном тесте вы, вероятно, хотите использовать [NSBundle bundleForClass:[self class]]
, а не [NSBundle mainBundle]
. Это потому, что unit тест не является отдельным приложением. С 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")
}
Ответ 2
В Свифте
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)")
* Это включает в себя ответ тома Харрингтона, который находится в Задаче C