Xcode Unit Testing - Accessing Resources from the application's bundle?
- by Ben Scheirman
I'm running into an issue and I wanted to confirm that I'm doing things the correct way.
I can test simple things with my SenTestingKit tests, and that works okay. I've set up a Unit Test Bundle and set it as a dependency on the main application target. It successfully runs all tests whenever I press cmd+B.
Here's where I'm running into issues. I have some XML files that I need to load from the resources folder as part of the application. Being a good unit tester, I want to write unit tests around this to make sure that they are loading properly.
So I have some code that looks like this:
NSString *filePath = [[NSBundle mainBundle]
pathForResource:@"foo" ofType:@"xml"];
This works when the application runs, but during a unit test, mainBundle points to the wrong bundle, so this line of code returns nil.
So I changed it up to utilize a known class like this:
NSString *filePath = [[NSBundle bundleForClass:[Config class]]
pathForResource:@"foo" ofType:@"xml"];
This doesn't work either, because in order for the test to even compile code like this, it Config needs to be part of the Unit Test Target. If I add that, then the bundle for that class becomes the Unit Test bundle. (Ugh!)
Am I approaching this the wrong way?