Why can't code inside unit tests find bundle resources?

Viewed 58759

Some code I am unit testing needs to load a resource file. It contains the following line:

NSString *path = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"txt"];

In the app it runs just fine, but when run by the unit testing framework pathForResource: returns nil, meaning it could not locate foo.txt.

I've made sure that foo.txt is included in the Copy Bundle Resources build phase of the unit test target, so why can't it find the file?

7 Answers

if you have multiple target in your project then you need to add resources between different target available in the Target Membership and you may need to switch between different Target as 3 steps shown in the figure below

enter image description here

I had to ensure this General Testing checkbox was set this General Testing checkbox was set

Just for people like me, that missed this point in the original post:

Make sure that foo.md is included in the Copy Bundle Resources build phase of the unit test target

enter image description here

Related