Search code examples
objective-cunit-testingxcode4nsbundle

cant load files from NSBundle in iphone unit test


I'm using XCode 4.

Can someone please explain to me why when I have written a unit test (under the test target) that it can't find my local file resource? I'm trying to load a plist file from the NSBundle in a unit test but it doesn't work in the unit test target. I've tried throwing it under the Classes area of the Test target.

Here's my code:

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"urlMappings.plist"];
self.urlMappings  = [NSDictionary dictionaryWithContentsOfFile:finalPath];

self.urlMappings is empty for some reason even though urlMappings.plist definately exists in my project. Here is the source to urlMappings.plist to prove it is in fact a dictionary.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>%server%view.html</key>
    <string>testHtml1.html</string>
</dict>
</plist>

Any ideas? Thankyou


Solution

  • Under the test target, you shouldn't use [NSBundle mainBundle], because it is the main bundle, not the test bundle. you should use [NSBundle bundleForClass:[self class]] replace.