I have a schema in Xcode which has only Test configured. And this schema points to a target which has all my tests (this is Cocoa Unit Testing Bundle target for OS X).
So, I do Command+U to run all these tests and it works fine. However, couple of tests require access to localization resources. I added these resources to my target and even check in resulting built binary that localization resources are there.
However, the code doesn't see resources, so when any test does NSLocalizedString, it returns a key, instead of localized string.
Is there anything special what I need to do to let tests see these resources?
The post date on this question is kinda old, but I ran into this same problem. I found this awesome writeup on a blog which described this problem and has a great solution.
He has two solutions, the first one (didn't work for me):
replace
[NSBundle mainBundle]
with
[NSBundle bundleForClass:[self class]]
Second solution (worked very well which uses OCMock):
static id _mockNSBundle;
+(void)setUp {
_mockNSBundle = [OCMockObject niceMockForClass:[NSBundle class]];
NSBundle *correctMainBundle = [NSBundle bundleForClass:[self class]];
[[[[_mockNSBundle stub] classMethod] andReturn:correctMainBundle] mainBundle];
}
+(void)tearDown {
[_mockNSBundle stopMocking];
_mockNSBundle = nil;
}
Hope this helps people who checks this post out in the future.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With