Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add/open a bundle file in a test target

I have a static library that gets linked to by an application. The library code opens a file that is in bundle that is in the application bundle, the opening is done as:

NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"Config" ofType:@"plist"];

This is working fine.

However I want to add some unit test code to the library, and so I have a logic test target for it. As the file is in the bundle for the application and not in the bundle for the static library I copied the Config.plist file and added it to the test code target via Copy Bundle Resources. However when I execute the test code the file cannot be found. Why is that?

In that the above is confusing, here's a summary of the workspace structure.

Workspace contains:
    Application Project with application target, which contains (X)
        Config.plist (a)
    Library project which contains:
        Library target, which contains:
            the code opening the file in the bundle (b)
        Test library target, which contains: (Y)
            A Copy of the Config.plist (c)

So if I build X then when b runs it can find a. But when I build Y when it runs then b can't find c.

like image 631
Gruntcakes Avatar asked Apr 30 '13 23:04

Gruntcakes


1 Answers

I found if I changed [Bundle mainBundle] to [NSBundle bundleForClass:[self class]] then it works in both cases

like image 189
Gruntcakes Avatar answered Sep 19 '22 02:09

Gruntcakes