1

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

Mike S
  • 4,092
  • 5
  • 35
  • 68
  • Building off of MikeSimmons's solution, I came up with the following [Xcode: TEST vs DEBUG preprocessor macros](http://stackoverflow.com/questions/6748087/xcode-test-vs-debug-preprocessor-macros/6763597#6763597). – ma11hew28 Jul 20 '11 at 14:40

2 Answers2

7

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.

jinhua liao
  • 286
  • 3
  • 10
0

The solution (which doesn't make sense) is it wasn't in the bundle resources of the main target. It has to be in the bundle resources of BOTH targets even though I'm only using it in test.

Anyone know why this is the case?

Mike S
  • 4,092
  • 5
  • 35
  • 68