Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode Unit Test and Writing Data to Disk

I am trying to write logic test to debug a a method that saves image data to disk. I use the following lines to write image data to the documents directory:

docspath = [[NSString alloc] initWithString:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, TRUE) objectAtIndex:0]];
fullpath = [[NSString alloc] initWithFormat:@"%@/%@", docspath, _name]; 
BOOL written = [fileData writeToFile:fullpath options:NSDataWritingAtomic error:&dataerror];

Except, the data is not written and the error description returns no such file or directory. I am running this code under a test target in the iOS 5.1 simulator. Do I need to create intermediate directories to get to the documents directory, or is there another issue with writing to disk under a test target?

The error returned:

2012-06-29 16:13:31.819 otest[4597:7b03] /Users/myaccount/xcode_projects/myproject/myproject/myfolder/MMFileManager.m - 285: Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)" UserInfo=0x1e1e3a0 {NSUserStringVariant=Folder, NSFilePath=/Users/myaccount/Library/Application Support/iPhone Simulator/5.1/Documents/mmLogo.png, NSUnderlyingError=0x1e1b430 "The operation couldn’t be completed. No such file or directory"}
like image 881
Mike D Avatar asked Feb 20 '23 00:02

Mike D


1 Answers

Unit test is not in your app, therefore, you can't access Documents directory in the unit test.

refer to NSHomeDirectory in iPhone unit test

like image 168
user200778 Avatar answered Mar 05 '23 23:03

user200778