Search code examples
iphoneiosunit-testingnsbundle

Able to load file from app bundle but not from test app bundle


To quickly test something I added a test file to my app's bundle and loaded it in the delegate using NSBundle:pathForResource. This worked.

However now I've just created a target of type Unit Testing and added the file and code to this new target but am unable to load it.

I've added the file to the test target using Copy Bundle Resources, and this is the full code for loading it:

NSBundle *mainBundle = [NSBundle mainBundle];
NSString *xmlFilePath = [mainBundle pathForResource:@"TestServerResponse" ofType:@"xml"];
STAssertNotNil(xmlFilePath, @"Unable to open file TestServerResponse.xml")

I can't figure out why I could load it when it was part of the app bundle target, but now I've moved it to the test bundle it won't load.


Solution

  • Try this one:

    NSBundle *testBundle=[NSBundle bundleForClass:[self class]];
    NSString *testBundlePath=[testBundle pathForResource:@"TestServerResponse" ofType:@"xml"];
    

    For test-cases, the main-bundle or your app is not the main bundle.

    You need to access bundle for your class yourself.