Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localizations aren't loaded in XCTest?

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?

like image 465
Victor Ronin Avatar asked Nov 08 '13 00:11

Victor Ronin


1 Answers

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.

like image 55
jc555 Avatar answered Sep 20 '22 11:09

jc555