3

I'm trying to unit-test code for loading and querying data from plist file. I'm loading data with code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"availableshops" ofType:@"plist"];
NSArray *arrayOfShops = [[NSArray alloc] initWithContentsOfFile:path];

When testing app in simulator and on the device everything work. BUT when I run unit test, code "[arrayOfShops count]" always returns "0".

I have the same files in "Copy Bundle Resources", "Compile Sources" and "Link Binary With Libraries" for main target and unit-test target. I've also tried with and without main target as "Direct Dipendencies" for unit-test target.

Does anyone have idea what is the problem?

Uroš Milivojević
  • 203
  • 1
  • 2
  • 10

3 Answers3

2

You cannot use [NSBundle mainBundle] with Xcode Unit Test, that will work if you use this instead:

NSBundle *bundle = [NSBundle bundleForClass:[self class]];

NSString *path = [bundle pathForResource:@"availableshops" ofType:@"plist"];
NSArray *arrayOfShops = [[NSArray alloc] initWithContentsOfFile:path];
Ludovic Landry
  • 11,606
  • 10
  • 48
  • 80
1

See this answer. Apparently mainBundle doesn't work in the context of a test bundle.

Community
  • 1
  • 1
Frank Schmitt
  • 25,648
  • 10
  • 58
  • 70
0

Are you sure the plist root element is an array? Posting the plist might help.

You could also try this:

NSArray* shopsArray = [NSArray arrayWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"availableshops.plist"];
Shaun
  • 311
  • 2
  • 7