简体   繁体   中英

OCUnit - iOS 5 - How do I test files in my app?

I am trying to build simple tests to see if files I need (images, videos, audio files) are in my app. Whenever I try to do something such as:

if (![[NSFileManager defaultManager] fileExistsAtPath:@"Menu Loop.mp3"])
{ 
    STFail(@"File does not exist");
}
else
{
    NSLog(@"Passed");
}

It says the file does not exist. I can see the file is there, and I know it is failing because the file in the app folder and not in the test folder. Is there a way of getting out of the test folder to test if the files exists without adding all of the files to the test folder? I have tried doing something such as fileExistsAtPath:@"../appfolder/Menu Loop.mp3" to try to navigate out of the test folder, but it unfortunately does not work that way either.

Thanks in advance!

You cannot directly access folders like that through the iOS SDK. You must save and read files using the system functions that return the proper path:

NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
NSString *pathToMenuLoopFile = [rootPath stringByAppendingPathComponent:@"Menu Loop.mp3"];

If you are looking to load files that you include in your Xcode project, please take a look at this post: Loading data files in iPhone project

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM