Created
January 29, 2012 08:49
-
-
Save TonnyXu/1697924 to your computer and use it in GitHub Desktop.
Use bundleForClass to get the unit test bundle's path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This tips is extremely useful when you test your CoreData app in Unit Test. | |
*/ | |
// iOS app is an executable bundle, means the `main` method is | |
// included inside app bundle, so `mainBundle` returns the app bundle | |
// | |
// See doc at https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40003624 | |
NSBundle *appBundle = [NSBundle mainBundle]; | |
NSString *appBundlePath = [appBundle bundlePath]; | |
// iOS unit test bundle is not an executable bundle, it is invoked by | |
// OCUnit, which is an executable bundle, so you can only get the bundle | |
// path by `bundleForClass` | |
// | |
// see doc at: | |
NSBundle *testBundle = [NSBundle bundleForClass:[YourTestClass class]]; | |
NSString *testBundlePath = [testBundle bundlePath]; | |
NSBundle* bundle = [NSBundle bundleForClass:[OCTestTests class]];
NSString* testBundlePath = [bundle bundlePath];
NSString* path = [bundle pathForResource:@"Info" ofType:@"plist"];
NSLog(@"path = %@", path);
// path = /Users/nagano/Library/Developer/Xcode/DerivedData/OCTest-edbutzmpyheqvyfvkyfooctdgljv/Build/Products/Debug-iphonesimulator/OCTestTests.octest/Info.plist
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The link inside gist is not clickable, click it here:
NSBundle
Class Reference