Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode writing to file in a unit test

I understand that when a unit test executes, it is in the sandbox environment of Xcode. In a unit test, I need to write and read data to a file. Since the project resides in a git repository, this must be done completely from within code (i.e. no special manual test setup of directories is possible)

For all my searching and trying today, I cannot find a way to write to a file from within the unit test.

Does anybody know of a method to write to a file under these conditions?

Edit: This is the problematic code:

let dirPath = NSTemporaryDirectory()!

var filePath = dirPath.stringByAppendingPathComponent("unittest.json")

if !NSFileManager.defaultManager().isWritableFileAtPath(filePath) {
    return (nil, "Directory missing or write access not granted for \(path)")
}
like image 911
Rien Avatar asked Jan 19 '15 17:01

Rien


1 Answers

You should be able to use the temporary directory: NSTemporaryDirectory() and have it reliably point to a safe place to read and write. I'd suggest making sure you clean up any created files.

let dirPath = NSTemporaryDirectory()

To see whether a file can be created (per the updated question), check the directory for write permissions:

let canCreate = NSFileManager.defaultManager().isWritableFileAtPath(dirPath)
like image 143
David Berry Avatar answered Oct 17 '22 00:10

David Berry