- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
目前,我正在尝试在单元测试期间创建文件,看看是否可以在创建后检索数据,但 fileManager.createFileAtPath 总是失败。我看了这个帖子:fileManager.createFileAtPath always fails我实现了该答案的解决方案,但仍然失败。这是单元测试和附带函数的样子:
internal func getCacheFolderPath() -> String {
var folderPath = ""
guard let path = self.urls(for:.cachesDirectory , in: .userDomainMask).last?.path else {
return folderPath
}
folderPath = path + "/NetworkCache"
if !self.fileExists(atPath: folderPath) {
do {
try self.createDirectory(atPath: folderPath, withIntermediateDirectories: false, attributes: nil)
} catch {
print(error.localizedDescription)
}
}
return folderPath
}
func test_createFile(){
let path = sut?.getCacheFolderPath()
let fileManager = FileManager.default
if !FileManager.default.fileExists(atPath: path!) {
do {
try FileManager.default.createDirectory(atPath: path!, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
print(error.localizedDescription)
}
}
let isFileCreated = fileManager.createFile(atPath: path!, contents: mockData, attributes: nil)
print(isFileCreated)//This always returns false
}
最佳答案
使用 data.write(to:) 而不是创建文件有助于确定问题所在。看起来我正在创建一个文件夹作为文件路径并尝试将文件保存到该路径。由于它是一个文件而不是文件夹,因此它返回那里已经存在某些内容。
以下是完整的工作代码:
func saveData(data: Data, forFileName fileName: String, completionHandler: @escaping (URL?)->()) {
let url = URL(fileURLWithPath: self.filePathForFileName(name: fileName))
print("saveData:\(url)")
do {
// [data writeToFile:[self thdncFilePathForFileName:name] atomically:YES];
try data.write(to: url, options: .atomic)
completionHandler(url)
} catch {
print(error.localizedDescription)
completionHandler(nil)
}
}
func test_createFile(){
var isSaved: Bool = false
let path = sut?.getCacheFolderPath()
let promise = expectation(description: "Completion Hander invoked")
if !FileManager.default.fileExists(atPath: path!)
{
do {
try FileManager.default.createDirectory(atPath: path!, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
print(error.localizedDescription)
}
}
sut?.saveData(data: mockData!, forFileName: filePath, completionHandler: { (url) in
let fileManager = MockFileManager.default
if fileManager.fileExists(atPath: self.sut!.filePathForFileName(name: self.filePath)) {
isSaved = true
print("FILE AVAILABLE")
} else {
isSaved = false
print("FILE NOT AVAILABLE")
}
promise.fulfill()
})
wait(for: [promise], timeout: 10, enforceOrder: true)
XCTAssertTrue(isSaved)
}
关于ios - fileManager.createFileAtPath 在单元测试期间失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49619370/
目前,我正在尝试在单元测试期间创建文件,看看是否可以在创建后检索数据,但 fileManager.createFileAtPath 总是失败。我看了这个帖子:fileManager.createFil
我尝试使用 NSFileManager 和方法 createFileAtPath 创建一个 PLIST 文件。最后,文件被创建,它的大小为 0 字节,我什至可以在 Finder 中看到该文件的特定 P
我尝试调用 fileManager.createFileAtPath 方法,但总是失败。我的变量 success 始终为 false。我在这里查看了一些类似的帖子,但没有一个完全符合我的需要。这是我的
我在将一些数据保存到文件时遇到问题。下面一行返回一个 NO: [self.fileManager createFileAtPath:fileString contents:photoData attr
无论如何,是否可以从“createFileAtPath”中获取更详细的错误数据?我有点期待 NSError?目前我使用的是 BOOL 返回值。 success = [fileMan createFil
在这种情况下,下面 block 中的 if/else 怎么可能不断返回“找不到文件”?我已经在 iOS 7 和 iOS 8 上的设备上进行了测试,每次都无法在假定创建文件后立即检测到文件……难倒了。
我正在尝试为我的应用程序的每次执行编写一个简单的日志文件。日志文件名以当前时间为准,保存在app目录下。下面的代码是我正在尝试的,它可以在模拟器上运行,但是当我在 iPad 上执行时,它失败并显示“不
也许我只是遗漏了文档中的某些内容,但我找不到任何说明此行为应该在 iOS 8 中更改的内容。 我的应用程序将当前工作目录设置为 Documents 目录,然后尝试使用 NSFileManager -c
在上千次 print() 语句之后,我终于找到了问题所在!但是,我不确定如何修复它。问题出在这一行: NSFileManager.defaultManager().createFileAtPath(s
所以我试图找出为什么我无法制作可读的文件副本。我尝试过 moveItemAtPath、copyItemAtPath 和 createFileAtPath。每次,从原始路径读取数据工作正常,但从新文件路
在我的 iPhone 应用程序中,我能够将数据导出到 .txt 文件并通过邮件发送 - 但目前在编码方面存在问题。 要将文件附加到邮件,我只需从 NSString 实例创建一个 NSData 实例,如
我是一名优秀的程序员,十分优秀!